Friday, 6 October 2017

c++ - Double free or corruption when using destructor

In the following code when I add the the line which is specified with an arrow gives error:




Error in `./a.out': double free or corruption (fasttop):
0x00000000007a7030 * Aborted (core dumped)




The code works if I do not use destructor. Any idea?




#include
#include

struct Element
{
int *vtx;

~Element ()
{
delete [] vtx;

}
};

int main ()
{
Element *elm = new Element [2];
elm[0].vtx = new int [2]; // <----- adding this gives error

std::vector vec;
vec.push_back (elm[0]);

vec.push_back (elm[0]);

return 0;
}

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...