3

I can visualize the situation when, for example, I have allocated memory in the following way:

Position* arr1 = new Position[5];

Position being a class in my program that describes a positional point with x and y values.

There would be a pointer on the stack that points to the first element (a Position object) of the array "arr1" on the heap so it would look something like this:

enter image description here

How would it look if I were to create an array of pointers though? For example:

Position** arr2 = new Position* [2];

arr[0] = new Position(3, 7); // Constructs a point with x and y values.
arr[1] = new Position(9,6);

Where are all the pointers and objects stored in the memory in my second example? Are there pointers on the stack that point to pointers on the heap which then point to the objects on the heap or something?

Also, if I were to delete [] arr2;, where would the objects remain in the memory?

Thanks.

4 Answers 4

3
  1. Every object created by new is on the heap. So in your example, the array of two pointers would be on the heap. arr2, as a local variable, would be on the stack.
  2. The Position objects, being separately allocated by new, are in their own separate heap blocks. They need to be deleted individually. Otherwise, they remain on the heap. An object cannot move between the stack and heap. (Although C++11 provides std::move, this is really a special kind of ownership transfer; it does not magically turn a heap address into a stack address.)
  3. delete [] arr2 would leave the objects in memory. As the array of pointers does not own the referenced objects, this code does not ask to destroy the indirectly referenced objects.

You should always avoid using new and delete directly in favor of container objects (std::vector or std::array would work here), or smart pointers such as unique_ptr and shared_ptr.

Sign up to request clarification or add additional context in comments.

Comments

2

You have one automatic variable, arr2 pointer object. The rest is on the free store. If you only delete[] arr2, you'll free the memory for the pointers, and not the objects they point to.

arr2 ---+  +---------------------------------+
        |  |                                 |
        |  |                                 |
        +----> [Position*, Position*]        |
           |       |          |              |
           |       |          +----> [9, 6]  |
           |       v                         |
           |    [3, 7]                       |
           +---------------------------------+

1 Comment

Can I ask how do you make this frame in ascii?
1

The pointer of the array arr2 will be on the stack. The other pointers arr2[0], arr2[1] etc will be on heap and these pointers will point to different locations on the heap.

Comments

0

Your diagram would have an extra level of indirection. So replace myArray with arr2, having arr2[0] and arr2[1]. Then, still on the heap, draw another arrow from arr2[0] to a Position value, and from arr2[1] to another Position value.

If you used delete[] arr2, these values would remain at whatever position in memory they were assigned originally.

2 Comments

You are suggesting a change to his design. His diagram does match his code (the first example).
I was pointing out how you would modify it to represent the 2nd example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.