Learn C++ Online

Dynamic allocation of objects on the free store (part 1)

On this page
This lesson contains approximately 28 minutes of video content.

Memory allocation review

In my diagram, I write free store (aka heap). Unfortunately, this equivalence isn't entirely correct: there is a logical distinction between the two. The free store is to C++, as the heap is to C. Many C++ programmers consider objects dynamically allocated and released using C (not C++) "conventions" (e.g., malloc) as heap objects, and objects allocated using C++'s new or new[] and released using delete or delete[] as free store objects.

Dynamically allocated objects

What's on the stack? What's in the free store?

The different flavors of the delete operator

Questions

Q1

QFree store, Q1

This question is ungraded.

You've created a new object on the free store and stored its address in a local pointer:

int* ptr = new int{7};
Write the C++ (semi-colon terminated) statement that deallocates the memory pointed to by ptr.

Q2

QFree store, Q2

This question is ungraded.

You've created a new object on the free store and stored its address in a local pointer:

int* ptr = new int[7];
Write the c++ (semi-colon terminated) statement that deallocates the memory pointed to by ptr.

Q3

QFree store, Q3

This question is ungraded.

Given the following statement,

std::string* ptr = new std::string("Howdy");
where is the memory allocated for the object ptr?

Q4

QFree store, Q4

This question is ungraded.

Given the following statement,

std::string* ptr = new std::string("Howdy");
where is the memory allocated for the object pointed to by ptr?

Q5

QFree store, Q5

This question is ungraded.

Given the following statement,

double* ptr = new double;
what is the starting value of the object pointed to by ptr?