Thread: Easy pointer question

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    8

    Easy pointer question

    Hi all,

    This is a great site. I'm doing the tutorials now, and I ran into a minor hitch:
    The delete operator frees up the memory allocated through new. To do so, the syntax is as in the example.

    Code:
    delete ptr;
    After deleting a pointer, it is a good idea to reset it to point to 0.
    Deleting the pointer before resetting it to 0 gives me this error:
    Debug Assertion Failed!
    Expression: _BLOCK_TYPE_IS_VALID(pHead -> nBlockUse)

    Resetting the pointer to 0 before I delete it seems to work, but I'm wondering - is there more to this than meets the eye?

    code:
    Code:
    int main()
    {
    	int pointee = 1010;
    	int *pointer = new int;
    	cout<<"\nPointee is " << pointee << "";
    	pointer = &pointee;
    	cout<<"\nPointer is " << pointer << "";
    	pointer = 0;
    	cout<<"\nPointer is " << pointer << "";
    	delete pointer; //if I try deleting pointer before setting it to 0, I get the error 
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Edo
    Deleting the pointer before resetting it to 0
    You actually use delete after setting the pointer to 0 (which is wrong), but delete on a null pointer should have no effect, so I am not sure why you get such an error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int main()
    {
    	int pointee = 1010;
    	int *pointer = new int;
    	cout<<"\nPointee is " << pointee << "";
    	pointer = &pointee;
    	cout<<"\nPointer is " << pointer << "";
    	delete pointer; //if I try deleting pointer before setting it to 0, I get the error 
    }
    This is what crashes, right?

    That's because the red line changes what your pointer points at, so that it now points at your local variable pointee. That is not what you got back from new, so you can't delete it.

    It would be like you take a book out of the library, and then return a completely different book - it doesn't matter if it's a "better" or "crappy" book you give back, you are not returning what you borrowed, so you are likely to get the "pay for lost books" penalty - which is roughly what that error message means.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    8

    (solved)

    Quote Originally Posted by matsp View Post
    . . .the red line changes what your pointer points at, so that it now points at your local variable pointee. That is not what you got back from new, so you can't delete it.

    It would be like you take a book out of the library, and then return a completely different book
    Thanks, that was a great analogy! Now I understand what went wrong, and I'll delete the pointer first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy Pointer question
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-30-2008, 08:21 PM
  2. Question about pointer arithmetic and types
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 01:53 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 09:03 AM
  5. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM