C++ program crashes


 
Thread Tools Search this Thread
Top Forums Programming C++ program crashes
# 1  
Old 02-10-2010
C++ program crashes

Hi,

Could anyone tell me the reason why the following program crashes?

Code:
class A {
    int x;
public:
    A() {
        cout << "from A()" << endl;
    }

    ~A() {
        cout << "from ~A()" << endl;
    }
};

class B : public A {
public:
    B() {
        cout << "from B()" << endl;
    }

    virtual ~B() {
        cout << "from ~B()" << endl;
    }
};

int main() {
    A* a = new B;
    delete a;
}

when I drilled down the core-dump, I got *** glibc detected *** a.out: free(): invalid pointer: 0x0804a00c ***

I know the base class destructor need to be declared "virtual". But what is the exact reason for crashing?
# 2  
Old 02-10-2010
That's going to be implementation-specific, since it depends on how your compilers' virtual tables work. bottom line, freeing the wrong pointers because you're freeing the wrong type.
# 3  
Old 02-10-2010
I got a reply in another forum as below which I would like to share here.

Sizeof(A) = 4 bytes
Sizeof(B) = 8 bytes

When you assign an instance of derived class object to its base class pointer, then the base class pointer points to the base part of the derived class object.
On freeing, it tries to free 8 bytes but only 4 bytes allocated for base part.

If you want to delete the object, delete a object with a pointer to its actual type as

B* b = new B;
A* a = b; // you can use the object via either b or a but
delete b; // delete as b
b = null;

Last edited by royalibrahim; 02-10-2010 at 08:18 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Apache2 Crashes

The Apache server suddenly stops. I am running Debian Jessie Here are some diagnostics: root@meow:/var/www# apachectl configtest AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress... (4 Replies)
Discussion started by: Meow613
4 Replies

2. UNIX and Linux Applications

Apcupsd crashes

I am trying to run apcupsd, but it will not retain contact the UPS root@meow:/home/ethan/UPS/gapcmon-0.8.9# /etc/init.d/apcupsd start Starting UPS power management: A copy of the daemon is still running. If you just stopped it, please wait about 5 seconds for it to shut down.... (0 Replies)
Discussion started by: Meow613
0 Replies

3. Programming

C++ program crashes

Hi, Can anyone tell me why the below program is crashing? and where exactly it is crashing. What is the corrective measure to be taken to make it work fine? #include <iostream> #include <cstring> using namespace std; class CString { char* m_data; public: CString() :... (6 Replies)
Discussion started by: royalibrahim
6 Replies

4. Programming

Program crashes on calling __libc_msgrcv()

Hi, I am a newbie to linux programming. I have implemented msgqueue in C. msgrcv() call at the client end is as below: msgrcv( msgqid, msgptr, msgsize, msgtype, 0 ); My program works fine when msgrcv () from /lib/libc.so.6 is called. However it crashes when __libc_msgrcv() is called. ... (3 Replies)
Discussion started by: praasanna
3 Replies

5. Red Hat

7z crashes system

Can someone tell my why every time I try to use 7z it freezes my system? I can't move my mouse, I can't type, I can't kill my xsession. I then restart my system and everything returns to normal. When I try to use 7z my system again freezes. (11 Replies)
Discussion started by: cokedude
11 Replies

6. Solaris

Server crashes when not in use.

Production server crashes when there is no traffic on it. Can only recover by going to ALOM and reboot the server. This seems to happen about the same time every month. The only good thing is the server is not in production at the time of the crash. I have been unable to locate any information in... (5 Replies)
Discussion started by: Joeentech
5 Replies

7. SuSE

Chromium flashes and crashes

I am running openSUSE 11.2 with KDE4.5 on my eMachines e525. I just did a "zypper up" on my system and it reported that Chromium was going to be updated. I agreed and the when the update was finished I shut down Chromium and started it again. It flickered on the screen for a moment and crashed.... (6 Replies)
Discussion started by: Druonysus
6 Replies

8. Linux

gethostnameby_r crashes

Hello all, I'm trying to use gethostbyname_r function with 6 arguments in one of my functions. But the call to this crashes the program. Kindly help me in resolving this... Compiler Info: Linux target: i686-hardhat-linux version: 3.3.1 The following is the piece of code I'm trying to... (1 Reply)
Discussion started by: rajans
1 Replies

9. Programming

AIX 5.3 64-bit program crashes with AIX 5.1

I have an AIX 64-bit program which uses following from AIX5.3 /usr/lib/libc.a(shr_64.o) /usr/lib/libpthread.a(shr_xpg5_64.o) /home/jeet_xp/export/power/usr/lib/libsarpc.a(shr.o) /unix /usr/lib/libcrypt.a(shr_64.o) /usr/lib/libc_r.a(shr_64.o) ... (3 Replies)
Discussion started by: jeet_xp
3 Replies

10. HP-UX

Program crashes with optimization level O2

I am experiencing a difficulty undersatnding why my program (C++, HP UNIX) crashes. It crashes only when I build it with -O (+O2) optimization switch (used in aCC compiler). It works ok with +O0 or +O1 optimization. Also, I see that local variables are shown incorrecly when program is built... (3 Replies)
Discussion started by: Yuriy07
3 Replies
Login or Register to Ask a Question