Take a look at this program.

#include <iostream>
using namespace std;

struct Node
{
    int data;
    Node* link;

    Node()
    {
        data = 0;
        link = NULL;
    }
};

void destroy(Node* &p)
{
    Node* pre;

    while(p !=NULL)
    {
        pre = p;
        p = p->link;
        delete pre;   // When running for the second time, pre value becomes
                      // 0x602030 but when the statement executes , it doesn't
                      // cause a SIGABRT
    }
}

int main()
{
    Node* ints;
    ints = new Node; // link a node

    ints->link = new Node; // link another node

    delete ints->link; // Delete the second node (value: 0x602030)
    // If I run the above statement again in main, it will casue a SIGABRT
    //signal with the following message. media/<My project location>/dist/
    //debug/ cppapp: double free or corruption (fasttop): 0x0000000000602030 ***

    destroy(ints);
    return 0;
}

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.

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.

:sad: 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. :yawn:

>That wasn't all that helpful.
Explanations of undefined behavior are rarely helpful. Just don't write code like that. ;)

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

>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++. ;)

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

-_-

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

well Sinaru, this problem is very simple. I will explain this in my next post.
Firstly, I need a little help from you.

#include <stdio.h>

int main (void )
{
  printf("Hello to DanWeb\n");

  return 1;
}

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

commented: It could be the broken English, but this strikes me as a very unproducetive response. -4

Well, sorry for say stopping. It should be dealt through adminstrator and modulator.
Thanks.
-Manoj

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.