Easy pointer question

This is a discussion on Easy pointer question within the C++ Programming forums, part of the General Programming Boards category; Hi all, This is a great site. I'm doing the tutorials now, and I ran into a minor hitch: The ...

  1. #1
    Edo
    Edo is offline
    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
    21,433
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Edo
    Edo is offline
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21