Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
int _tmain(int argc, _TCHAR* argv[])
{
    int *c= new int[5];
    c[0] = 100;
    c[1] = 200;
    c[2] = 300;
    c[3] = 400;
    c[4] = 500;
    int i=0;

    while(i<5)
    {
        printf("%d\n",*c++);
        i++;
    }

    delete[] c;
        return 0;
}


why it asserts Heap-Block-in-use when I delete the pointer? :confused:
Posted
Comments
chandanadhikari 2-Dec-12 8:08am    
hi,
think it simply means that you are deleting the pointer while the memory in heap is still allocated .so in this way you will loose the handle to the memory i.e. the pointer hence rendering the location inaccessible.

When you do your while loop, you increment c pointer so at the end is not pointing to the originally allocated allocation anymore.

You have to use a temporary variable Inside the loop;

C++
...
int i=0;
int *t = c;

while(i<5)
{
    printf("%d\n",*t++);
    i++;
}
...
 
Share this answer
 
Comments
Espen Harlinn 2-Dec-12 8:24am    
Right :-D
Thanks a lot.
How does it keep track that this pointer will run to these many elements in memory?
For example assume I have a function like below

int* supplyPtr()
{
   int* pSet  = new int[10];
   return ++pSet;
}

void main()
{

 int* p = supplyPtr();
 delete[] p;

}

In the above sample how does it keep track of p?
when I return pSet from the supplyPtr, it cleanly deletes the memory.
But if I pass ++pSet, it asserts. How does it associate a pointer and it's number of elements in memory? Could you please explain? or point me to the right resource?
 
Share this answer
 
Comments
Richard MacCutchan 2-Dec-12 9:08am    
"It" does not keep track of anything, that is something your code must do. If you allocate memory using new then you must keep that pointer for use in your call to delete []. Note in the above code the pointer you return from supplyPtr() is already invalid as you have moved it beyond the start of its heap control block.
Smith# 2-Dec-12 9:22am    
Yes correct, but on a straight case, int* pInt = new int[5], how does the run time delete[] 5 elements in memory? or is it not doing actually?

In a single memory allocation like int* pInt = new int(10), the run time deletes the single value @ pInt thus freeing a memory location of single int value.

How does a delete[] know it should free 5 elements memory in an int[5]?

Richard MacCutchan 2-Dec-12 10:48am    
The size of the array is noted in the array's control block at the time of calling new. But the call to delete needs that original pointer in order to know how big that block is. The control block is managed by the memory allocator but it can only be found from the original pointer.
Nelek 2-Dec-12 13:38pm    
Please don't post solutions to add information, to ask something or to comment another user.
- To add information to your message, you can use the widget "Improve question" / "Improve solution" at the bottom of your text.
- To ask/answer a user, you can use the widget "Have a question or comment?" (as I am doing right now with you) or the widget "reply" in another comment.
[no name] 3-Dec-12 5:24am    
You make a good point and this comment applies to some high ranking members (not you) who have a very strange interpretion of what constitutes an answer.

Many do not seem to know the difference between a comment and an answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900