Junk pointer too high to make sense.

This is a discussion on Junk pointer too high to make sense. within the C++ Programming forums, part of the General Programming Boards category; I get the following message: free(): warning: junk pointer, too high to make sense. What does that mean? It is ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    Junk pointer too high to make sense.

    I get the following message:
    free(): warning: junk pointer, too high to make sense.

    What does that mean? It is called right before the call to delete. The
    deconstructor then does not get called. I've seen this happen before.

    Code:
    void A::operator=(const A &a)
    {
      delete this;
      copy(a);
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Why do you have delete this in there? That is wrong because it releases the memory used by the object. If you want to destroy the existing data in the class, create a cleanup method and call it from operator= and the destructor.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Quote Originally Posted by Daved
    Why do you have delete this in there? That is wrong because it releases the memory used by the object.
    Isn't that what I want to do? "this" is just a pointer to the called object, right?

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Oh, wait that doesn't make any sense. Thanks.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Depending on the member variables you have, you still might have to use delete to clean those up. You just never want to delete the actual object itself.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    But what does that message mean? I've seen this happen in C programs as well.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    I don't know. My guess is that you created the object locally on the stack. So the this pointer points to some memory location on the stack. Since you are only allowed to delete objects created on the heap with new, the code that releases the memory when you called delete looked at the memory value and realized it was too high to be from the heap and that something was wrong with the program. free() is just a function used by delete to free the memory.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    "delete this" (sometimes referred to as "Committing suicide") is generally a very bad idea, as it destroys the object without checking to see if it is still referred elsewhere in the program.
    There's a good chance you will accidentally leave references to your object dangling - even if you 'think' you've been careful, the next time you or someone else modifies your code, your or they may inadvertently call your 'suicide' function and then try to access the object. The result is undefined behaviour, and you may have a difficult time chasing bugs.

    If you think you need to delete this, You should put some serious consideration into a different approach to your program design. (Or you could investigate smart pointers)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. WinXP Upgrade Doesn't Make Any Sense
    By Troll_King in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-25-2001, 04:18 PM
  5. anyone make any sense of this
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 10-03-2001, 10:29 PM

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