Sir, when we are allocating 2D array using DMA, we are using pointer to a pointer variable using new operator, to allocate memory at runtime in the heap. And, when we have to delete this 2D array from the heap, we use delete operator. So, my doubt is that when we delete it from the heap, what it means? Is it mean, we have removed all the allocated address for this 2D array in the heap or just removed all the values present inside the 2D array!!! Because, after deleting this 2D array from the heap using delete operator, I tried to print this 2D array again and I have found some garbage values. So, I was confused that is it just delete all the values in the 2D array and taking garbage values in the same allocated address in the heap or is it just iterating the two loops for printing 2D array and taking some garbage values for any random addresses in the memory!!! So, I need to clarify, how this delete operator working internally. Thank you.
Dynamic Memory Allocation(DMA)
Hey @Kinjal
Using the delete operator on the 2D dynamically allocated array on the heap basically frees the pointers (the pointers to pointers and the main pointer). So basically those values are not stored and that space is not allocated anymore. But garbage values stay in that space and that space can be reallocated as it’s been freed now
I’m not sure though because it’s kinda paradigm. As you have said that all the pointer will be gone after using delete operator so now if any space will allocate for garbage values to print elements of 2D array again which I apparently did in my code, now these re-allocated addresses generated from stack not from heap. I suppose. Isn’t right?
@Kinjal
Delete operator definitely deletes the array data that’s stored on the heap. “Delete” here really just means that the memory that was allocated is now able to be reused for other purposes. It’s almost certainly still allocated to the program process and probably still has the old data in it (or at least some of the old data). The delete operator doesn’t delete the pointer itself since that’s a local variable stored on the stack. In fact, it should still contain the address that was assigned to it by the “new” operator.
[DMA]()
this is my code and at the end, i was again printing 2D array, after using delete operator. So, what your opinion, what will be printing? Yo’re pretty sure that random values. right!!!
Now, what about the addresses allocate by this random values, is it from the heap again or from stack, because after using delete operator, there is nothing about runtime allocation. Now, it’s all about compile time allocation. That’s where I stuck…Clarify me with your wisdom and excellence.