Crash when deleting object

I can not understand why does this code crashes. I know it should be delete[], but why is this code crashes when I just give the pointer?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

class Base
{
        public:
                Base():i_(0){std::cout<<"Base()\n";}
                virtual ~Base(){std::cout<<"~Base()\n";}
                void print(){std::cout<<"i="<<i_<<"\n";}
        private:
                int i_;

};

class Derived:public Base
{
        public:
                Derived(){std::cout<<"Derived()\n";}
                ~Derived(){std::cout<<"~Derived()\n";}
};

int main()
{
        Base* bp = new Derived[10];
        delete bp; //<------------crash in this line
}




Base()
Derived()
Base()
Derived()
Base()
Derived()
Base()
Derived()
Base()
Derived()
Base()
Derived()
Base()
Derived()
Base()
Derived()
Base()
Derived()
Base()
Derived()
~Derived()
~Base()
*** glibc detected *** ./deneme: munmap_chunk(): invalid pointer: 0x0987700c ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb75e1704]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb77c4231]
./deneme[0x8048abd]
./deneme[0x80488fe]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7588775]
./deneme[0x8048711]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:01 8378832 /tmp/deneme
08049000-0804a000 r--p 00000000 08:01 8378832 /tmp/deneme
0804a000-0804b000 rw-p 00001000 08:01 8378832 /tmp/deneme
09877000-09898000 rw-p 09877000 00:00 0 [heap]
b7571000-b7572000 rw-p b7571000 00:00 0
b7572000-b76ce000 r-xp 00000000 08:01 8395374 /lib/tls/i686/cmov/libc-2.9.so
b76ce000-b76cf000 ---p 0015c000 08:01 8395374 /lib/tls/i686/cmov/libc-2.9.so
b76cf000-b76d1000 r--p 0015c000 08:01 8395374 /lib/tls/i686/cmov/libc-2.9.so
b76d1000-b76d2000 rw-p 0015e000 08:01 8395374 /lib/tls/i686/cmov/libc-2.9.so
b76d2000-b76d5000 rw-p b76d2000 00:00 0
b76d5000-b76e2000 r-xp 00000000 08:01 8355905 /lib/libgcc_s.so.1
b76e2000-b76e3000 r--p 0000c000 08:01 8355905 /lib/libgcc_s.so.1
b76e3000-b76e4000 rw-p 0000d000 08:01 8355905 /lib/libgcc_s.so.1
b76e4000-b76e5000 rw-p b76e4000 00:00 0
b76e5000-b7709000 r-xp 00000000 08:01 8395378 /lib/tls/i686/cmov/libm-2.9.so
b7709000-b770a000 r--p 00023000 08:01 8395378 /lib/tls/i686/cmov/libm-2.9.so
b770a000-b770b000 rw-p 00024000 08:01 8395378 /lib/tls/i686/cmov/libm-2.9.so
b770b000-b77ef000 r-xp 00000000 08:01 2845093 /usr/lib/libstdc++.so.6.0.10
b77ef000-b77f3000 r--p 000e3000 08:01 2845093 /usr/lib/libstdc++.so.6.0.10
b77f3000-b77f4000 rw-p 000e7000 08:01 2845093 /usr/lib/libstdc++.so.6.0.10
b77f4000-b77fa000 rw-p b77f4000 00:00 0
b7813000-b7816000 rw-p b7813000 00:00 0
b7816000-b7817000 r-xp b7816000 00:00 0 [vdso]
b7817000-b7833000 r-xp 00000000 08:01 10347801 /lib/ld-2.9.so
b7833000-b7834000 r--p 0001b000 08:01 10347801 /lib/ld-2.9.so
b7834000-b7835000 rw-p 0001c000 08:01 10347801 /lib/ld-2.9.so
bfc66000-bfc7b000 rw-p bffeb000 00:00 0 [stack]
Aborted
You've perfmormed a vector new, but calling scalar delete. You need:
delete [] bp;
doesn't bp point to the first element of the array?
i just try to delete that element,so why does it crash?
You can't delete just one element like that.

It's all or nothing.
Topic archived. No new replies allowed.