简体   繁体   中英

Understanding Stack, Heap and Memory Management

int *ip = new int[10];
for (int i = 0; i<10; i++)
    *(ip+i) = i;

myfun(ip);  // assume that myfun takes an argument of
            //   type int* and returns no result

delete [] ip;

The above code is a small segment of a test function that I am trying to use to learn about the stack and the heap.

I am not fully sure what the correct sequence is.

This is what I have thus far:

  • When the pointer ip is created it points to a new int array of size 10 created on the heap due to to the "new" declaration.
  • 0-9 is added to the array from 0-9.
  • The pointer is now passed through to myfun which means that myfun has a pointer that points to the same memory space on the heap.
  • The delete []ip; removes the memory allocated on the heap to the ip pointer. The pointer that got passed through to myFun now points to nothing.
  • Once the function has finished the ip variable is deleted as it is only local to the function.

Would someone be able to clarify if I am correct or not and correct me where I went wrong? Also if I attempted to carry on using ip after that would it just point to nothing ?

I believe everything is correct, I have the following minor remark regarding the following bullet point;

  • The delete []ip; removes the memory allocated on the heap to the ip pointer. The pointer that got passed through to myFun now points to nothing.

It is not guaranteed that the pointer points to "nothing", in general it is recommended (see clarification in comment below this answer) that after your delete call, you initialize the pointer to NULL, ie ip = NULL in order to ensure the pointer points to nothing (instead of pointing to unallocated memory).

The sequence is correct except one point:

The delete []ip; removes the memory allocated on the heap to the ip pointer. The pointer that got passed through to myFun now points to nothing.

The pointer doesn't point to 'nothing' (ie isn't set to nullptr or 0 after freeing the memory). It just points to the same location which is now freed memory (ie memory marked as freed by the application and that can no longer be safely accessed). Accessing memory through that pointer would trigger undefined behavior .

One last notice: myfun might take the pointer by value or by reference. There are differences but your sentence would still be valid.

Everything is correct. But beware that calling delete doesn't delete anything but free the memory previously allocated, which means that your pointer contains the address that you mustn't use (dereferencing a free chunk of memory leeads to undefined behavior). The same for your stack variable, the memory associated to your local variable is not destroyed but released, so you mustn't try to use it. stack and heap are just two ways of memory management with two basic same operation (alloc/free).

So technically you can't say that your pointer points to anything, but it points to something that you are not authorized to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM