Delete keyword??

This is a discussion on Delete keyword?? within the C++ Programming forums, part of the General Programming Boards category; I am trying to delete a node in a linked list, and I get a seg fault like this: Code: ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    Delete keyword??

    I am trying to delete a node in a linked list, and I get a seg fault like this:

    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x0804da34 in SCLLRep::Merge (this=0x82499a0, set2=0x82499f0) at SCLL.cpp:187
    187                     delete set2_NodeToDelete;
    (gdb)
    Declared like this:

    Code:
    	SCLLNode* set2_NodeToDelete = new SCLLNode();
    	set2_NodeToDelete = set2->Getptr2last()->Getnextnode();
    (Tried without the new keyword too)

    -----

    What could be a possible problem that would cause this to result in a seg fault?

    The thing that confuses me is that it doesn't even execute anything. I just say "delete variable" and BAM, seg fault.
    Last edited by Paul22000; 07-10-2008 at 04:32 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Probably you try to delete something twice, or you try to delete an invalid pointer.
    But without any additional code, we can't say for certain.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    set2_NodeToDelete = set2->Getptr2last()->Getnextnode();
    In common linked lists, "next" after "last" is always NULL... If so, then you'd get a segfault if you try to delete that node, for sure.

    The thing that confuses me is that it doesn't even execute anything. I just say "delete variable" and BAM, seg fault.
    Yeah, but "delete" itself does "execute something", in the sense that it expands to:
    Code:
    this->~SCLLNode();
    deallocate(this);
    If "this" is not a valid pointer, it can go wrong in either of those two statements.

    --
    Mats
    Last edited by matsp; 07-10-2008 at 04:37 AM.
    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
    Apr 2008
    Posts
    167
    Quote Originally Posted by matsp View Post
    Code:
    set2_NodeToDelete = set2->Getptr2last()->Getnextnode();
    In common linked lists, "next" after "last" is always NULL... If so, then you'd get a segfault if you try to delete that node, for sure.


    Yeah, but "delete" itself does "execute something", in the sense that it expands to:
    Code:
    this->~SCLLNode();
    deallocate(this);
    If "this" is not a valid pointer, it can go wrong in either of those two statements.

    --
    Mats
    This particular list is circular. So next() is the first node in the list.

    Even so, it would give an error on the declaration line since it's null, rather than the delete.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, I doubt that the gcc implementation of delete is broken, so if you try to delete something and it crashes, then I'd say it's your code. Show us the code, and maybe we can lead you in the right direction even if we can't spot it directly.

    --
    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.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    It would crash before delete if set2->Getptr2last returns NULL, since deleting NULL is perfectly defined... but since this (probably) isn't the case, then there must be something else wrong.
    Perhaps an invalid pointer to freed storage or a pointer corrupted due to some memory corruption.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    It would crash before delete if set2->Getptr2last returns NULL, since deleting NULL is perfectly defined... but since this (probably) isn't the case, then there must be something else wrong.
    Perhaps an invalid pointer to freed storage or a pointer corrupted due to some memory corruption.
    Good point - NULL itself is safe to delete and won't call the destructor. So this turns into the more subtle problem of "overwritten data" or "double delete", both of which can lead to "mysterious" problems.

    Again, seeing the source would be beneficial.

    --
    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.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by matsp View Post
    Good point - NULL itself is safe to delete and won't call the destructor. So this turns into the more subtle problem of "overwritten data" or "double delete", both of which can lead to "mysterious" problems.

    Again, seeing the source would be beneficial.

    --
    Mats
    Well, there's a few thousand lines of code haha. The important thing is this:

    Code:
    class SCLLRep : public Set
    {
    	public:
    		SCLLNode *last;
    };
    Anywhere in a function within SCLLRep (even after setting last), doing "delete last" will seg fault. Makes no sense at all...

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unfortunately, unless you can reduce the problem to something smaller, you have to debug your 1000's of lines. Showing us 0.1% of the code will not tell us what is wrong. The pieces you show, are not wrong in themselves.

    --
    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.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Eureka!! I got it! Need to set the previous node to setnextnode(0) before delete will work!!!!!!

    WOW my brain is dead, that is a great stopping point, time to get some sleep finally!

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code:
    	SCLLNode* set2_NodeToDelete = new SCLLNode();
    	set2_NodeToDelete = set2->Getptr2last()->Getnextnode();
    So first you assign to the variable the address of a new SCLLNode object.
    Then you overwrite this address with whatever those calls return.
    You leak the object you allocated.
    You try to delete something entirely different.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by CornedBee View Post
    Code:
    	SCLLNode* set2_NodeToDelete = new SCLLNode();
    	set2_NodeToDelete = set2->Getptr2last()->Getnextnode();
    So first you assign to the variable the address of a new SCLLNode object.
    Then you overwrite this address with whatever those calls return.
    You leak the object you allocated.
    You try to delete something entirely different.
    Woops. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proper Usage of the delete Operator
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 04-26-2007, 12:53 AM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 02:52 PM
  4. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 11:17 PM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 01:45 PM

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