Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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 | :confused:
Posted 2-Dec-12 1:54am
Smith#1.3K
Comments
chandanadhikari at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
 
...
int i=0;
int *t = c;
 
while(i<5)
{
    printf("%d\n",*t++);
    i++;
}
...
  Permalink  
Comments
Espen Harlinn at 2-Dec-12 8:24am
   
Right :-D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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?
  Permalink  
Comments
Richard MacCutchan at 2-Dec-12 9:08am
   
"It" does not keep track of anything, that is something your code must do. If you allocate memory using <code>new</code> then you must keep that pointer for use in your call to <code>delete []</code>. Note in the above code the pointer you return from <code>supplyPtr()</code> is already invalid as you have moved it beyond the start of its heap control block.
Smith# at 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 at 2-Dec-12 10:48am
   
The size of the array is noted in the array's control block at the time of calling <code>new</code>. But the call to <code>delete</code> 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 at 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.
pwasser at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 545
1 Dnyaneshwar@Pune 500
2 ravikhoda 325
3 OriginalGriff 277
4 Damith Weerasinghe 265
0 Sergey Alexandrovich Kryukov 10,974
1 OriginalGriff 6,777
2 Peter Leow 4,785
3 Abhinav S 3,813
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web04 | 2.8.140421.2 | Last Updated 2 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid