My program dynamically allocates memory to an array of objects :

class particle

{

     public:

            

     int x[D],shifted,ID;

     

     particle()

     {

               shifted=0;

               ID=-1;

     }

         

};



// Array of all the particles

particle* particles;

int main()
{
   for(a=0;a<s_num;a++)

  {

     	 particles= new particle[num];
//some code
........
........
        for(i=num-1;i>=0;i--)

        {

		 delete (particles+i);
        }
   }
}

But it gave an error as follows ....

*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x09072f68 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7cc5604]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb7ea8231]
./a.out[0x804a6a6]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7c6c775]
./a.out[0x8048d11]
======= Memory map: ========
08048000-0804e000 r-xp 00000000 08:03 13130      /media/disk/c++/DLC/a.out
0804e000-0804f000 r--p 00005000 08:03 13130      /media/disk/c++/DLC/a.out
0804f000-08050000 rw-p 00006000 08:03 13130      /media/disk/c++/DLC/a.out
08050000-08059000 rw-p 08050000 00:00 0 
09071000-092c3000 rw-p 09071000 00:00 0          [heap]
b7c55000-b7c56000 rw-p b7c55000 00:00 0 
b7c56000-b7db2000 r-xp 00000000 08:05 50941      /lib/tls/i686/cmov/libc-2.9.so
b7db2000-b7db3000 ---p 0015c000 08:05 50941      /lib/tls/i686/cmov/libc-2.9.so
b7db3000-b7db5000 r--p 0015c000 08:05 50941      /lib/tls/i686/cmov/libc-2.9.so
b7db5000-b7db6000 rw-p 0015e000 08:05 50941      /lib/tls/i686/cmov/libc-2.9.so
b7db6000-b7db9000 rw-p b7db6000 00:00 0 
b7db9000-b7dc6000 r-xp 00000000 08:05 32641      /lib/libgcc_s.so.1
b7dc6000-b7dc7000 r--p 0000c000 08:05 32641      /lib/libgcc_s.so.1
b7dc7000-b7dc8000 rw-p 0000d000 08:05 32641      /lib/libgcc_s.so.1
b7dc8000-b7dc9000 rw-p b7dc8000 00:00 0 
b7dc9000-b7ded000 r-xp 00000000 08:05 50945      /lib/tls/i686/cmov/libm-2.9.so
b7ded000-b7dee000 r--p 00023000 08:05 50945      /lib/tls/i686/cmov/libm-2.9.so
b7dee000-b7def000 rw-p 00024000 08:05 50945      /lib/tls/i686/cmov/libm-2.9.so
b7def000-b7ed3000 r-xp 00000000 08:05 759861     /usr/lib/libstdc++.so.6.0.10
b7ed3000-b7ed7000 r--p 000e3000 08:05 759861     /usr/lib/libstdc++.so.6.0.10
b7ed7000-b7ed8000 rw-p 000e7000 08:05 759861     /usr/lib/libstdc++.so.6.0.10
b7ed8000-b7ede000 rw-p b7ed8000 00:00 0 
b7ef4000-b7ef8000 rw-p b7ef4000 00:00 0 
b7ef8000-b7ef9000 r-xp b7ef8000 00:00 0          [vdso]
b7ef9000-b7f15000 r-xp 00000000 08:05 34480      /lib/ld-2.9.so
b7f15000-b7f16000 r--p 0001b000 08:05 34480      /lib/ld-2.9.so
b7f16000-b7f17000 rw-p 0001c000 08:05 34480      /lib/ld-2.9.so
bfeef000-bff04000 rw-p bffeb000 00:00 0          [stack]
Aborted

Any idea where i m going wrong....

Recommended Answers

All 3 Replies

If you're deleting an array then I believe the syntax is delete []myArray. Here's an example that compiles..

#include <iostream>

using namespace std;

class Object
{
public:
    Object() { cout << "Object::Object()\n"; }
    ~Object() { cout << "Object::~Object()\n"; }
};

int main()
{
    Object* o = new Object[5];
    delete []o;
}

If you have an array and you delete it with the command: "delete array" then it still frees all of the memory because the memory was allocated in one big chunk. However, it does not call the destructors. Therefore, you want to use: "delete []array" instead which then tells it to delete an array and all the destructors get called.

>delete (particles+i);
You can only delete addresses that were returned by new. particles+i will only reference such an address if i is zero. The whole of particles can be released in a single statement (no loop necessary):

delete[] particles;

Your code suggests that you're confused about the difference between a dynamic array of objects and a dynamic array of pointers to objects.

commented: Good explanation +1
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.