The code below move the an element to top of an array
for ( i = j; i > 0; i-- ) {
myBlk *tmp = blks[i];
blks[i] = blks[i-1];
blks[i-1] = tmp;
delete tmp;
}
as the execution reaches delete tmp, I get:
*** glibc detected *** double free or corruption (out): 0x00007fffd556ad10 ***
If I remove that statement, there is no problem. But I don’t want memory to leak…
Promoting comment to answer.
It seems that you are confusing a memory allocation with a pointer copy. In your loop, you are not doing any memory allocation. You are just copying a pointer – which does not allocate memory.
So you should get rid of the
delete:deleteis only called when there is memory allocation – which you have none of. (none inside the loop at least)