943,014 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 504
  • C++ RSS
Sep 2nd, 2010
0

Why doesn't this cause a SIGABRT signal

Expand Post »
Take a look at this program.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node
  5. {
  6. int data;
  7. Node* link;
  8.  
  9. Node()
  10. {
  11. data = 0;
  12. link = NULL;
  13. }
  14. };
  15.  
  16. void destroy(Node* &p)
  17. {
  18. Node* pre;
  19.  
  20. while(p !=NULL)
  21. {
  22. pre = p;
  23. p = p->link;
  24. delete pre; // When running for the second time, pre value becomes
  25. // 0x602030 but when the statement executes , it doesn't
  26. // cause a SIGABRT
  27. }
  28. }
  29.  
  30. int main()
  31. {
  32. Node* ints;
  33. ints = new Node; // link a node
  34.  
  35. ints->link = new Node; // link another node
  36.  
  37. delete ints->link; // Delete the second node (value: 0x602030)
  38. // If I run the above statement again in main, it will casue a SIGABRT
  39. //signal with the following message. media/<My project location>/dist/
  40. //debug/ cppapp: double free or corruption (fasttop): 0x0000000000602030 ***
  41.  
  42. destroy(ints);
  43. return 0;
  44. }

I'm using debug mode and if I run "delete ints->link" statement twice in main, gdb will say the program cause a SIGABRT, when it hits the second delete statement because I free the same memory twice. But this doesn't happen when I run destroy method like in the above code.

In other words, According to my knowledge I'm freeing the same memory twice in the above program. It should cause a SIGABRT signal, right? but it doesn't. I need to know whether it's my mistake(ie. understanding in a wrong way or code error) or something wrong with the compiler?

I'm using g++ 4.4.3, Netbeans IDE, Ubuntu 10.4, gdb. I used gdb to see where the pointers point.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sinaru is offline Offline
18 posts
since Dec 2009
Sep 2nd, 2010
0
Re: Why doesn't this cause a SIGABRT signal
Memory errors are funny beasties. One reason they're so hard to debug is the symptoms are often unpredictable. So while I would be thrilled to get a SIGABRT, I wouldn't expect or rely on it.
Administrator
Reputation Points: 6442
Solved Threads: 1391
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 2nd, 2010
0
Re: Why doesn't this cause a SIGABRT signal
That wasn't all that helpful. I think it is a problem with a compiler because some time a ago I was trying to figure out why a simple cout statement will not show in the console in one of my testing programs. I was so desperate cause I thought it should be a stupid mistake. After giving up I thought to update the compiler and suddenly the cout statement is showing.
Last edited by Sinaru; Sep 2nd, 2010 at 10:22 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sinaru is offline Offline
18 posts
since Dec 2009
Sep 2nd, 2010
0
Re: Why doesn't this cause a SIGABRT signal
>That wasn't all that helpful.
Explanations of undefined behavior are rarely helpful. Just don't write code like that.
Administrator
Reputation Points: 6442
Solved Threads: 1391
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 2nd, 2010
0
Re: Why doesn't this cause a SIGABRT signal
I tried your code and got this

Aborted
[linux@localhost]$ echo $?
134
[linux@localhost]$

Note: I know that Ubuntu sets up its compiler slightly different than my Linux distro...
Last edited by gerard4143; Sep 2nd, 2010 at 10:41 am.
Reputation Points: 499
Solved Threads: 363
Postaholic
gerard4143 is offline Offline
2,189 posts
since Jan 2008
Sep 2nd, 2010
0
Re: Why doesn't this cause a SIGABRT signal
Click to Expand / Collapse  Quote originally posted by Narue ...
>That wasn't all that helpful.
Explanations of undefined behavior are rarely helpful. Just don't write code like that.
It's the way I test c++.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sinaru is offline Offline
18 posts
since Dec 2009
Sep 2nd, 2010
0
Re: Why doesn't this cause a SIGABRT signal
Click to Expand / Collapse  Quote originally posted by gerard4143 ...
I tried your code and got this

Aborted
[linux@localhost]$ echo $?
134
[linux@localhost]$

Note: I know that Ubuntu sets up its compiler slightly different than my Linux distro...

mine:
echo $?
0

-_-
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sinaru is offline Offline
18 posts
since Dec 2009
Sep 2nd, 2010
0
Re: Why doesn't this cause a SIGABRT signal
>It's the way I test c++.
Unfortunately, there are so many areas of undefined, unspecified, and implementation-defined behavior in C++ that empirical tests only go as far as the "it works for me" argument. You can test using varying compilers and varying systems for more portable results, though.
Administrator
Reputation Points: 6442
Solved Threads: 1391
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 3rd, 2010
-2
Re: Why doesn't this cause a SIGABRT signal
well Sinaru, this problem is very simple. I will explain this in my next post.
Firstly, I need a little help from you.
  1. #include <stdio.h>
  2.  
  3. int main (void )
  4. {
  5. printf("Hello to DanWeb\n");
  6.  
  7. return 1;
  8. }

Here is a simplest program I can made.
Now, the problem is I want to make a exe file of it without use any compiler.
We both are very different. We love to do the things in unconventional way.
Also, commercial C/C++ Compilers are very costly and free ones have no proper/Standard implementation ( you yourself are getting annoyed by there behaviour and without understanding that you start crying that was of no help to me).

Also, you like to remove headers , Libary or C/C++ API, then why use C++/C Compiler anyway.
Only you post the reply for this Query.
This or any other forum it takes give and take policy.
Now stop taking and start giving starting with this Query reply itself.
Be Thankful to whoever reply you because nobody is perfect here.
Stop this all yours stupid Queries once and for all.
-Manoj
Reputation Points: 6
Solved Threads: 8
Light Poster
manojwali is offline Offline
40 posts
since Sep 2010
Sep 3rd, 2010
0
Re: Why doesn't this cause a SIGABRT signal
Well, sorry for say stopping. It should be dealt through adminstrator and modulator.
Thanks.
-Manoj
Reputation Points: 6
Solved Threads: 8
Light Poster
manojwali is offline Offline
40 posts
since Sep 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Dynamic class overloads
Next Thread in C++ Forum Timeline: Casting problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC