Reputation: 1064
So valgrind is giving me this error:
Mismatched free() / delete / delete []
Which is referring to the following line of code:
delete[] data[position+num];
but the error keeps appearing even after i change it to
delete data[position+num];
the data array was allocated in the following manner:
data = new char*[width];
for (int i=0; i<width; ++i){
data[i] = new char;
}
I just want to delete that one column of (char **data).
Also, below that error i get the following:
Address 0x5a1c160 is 0 bytes inside a block of size 1 alloc'd
==21417== at 0x4C2B800: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
Upvotes: 0
Views: 4809
Reputation: 56547
Not sure what you mean by delete[] data[position+num];
. Your code also looks a bit dubious: data
is an array of pointers-to-char, i.e. array of C-like strings, but in the loop you allocate to each pointer a single char
via data[i] = new char;
. Are you sure that's what you want? Usually you allocate a bunch, data[i] = new char[some_length];
.
In any case, you need to delete
in reverse order of allocation:
for (int i=0; i<width; ++i){
delete data[i]; // or delete[] data[i] if pointing to more than one char
}
delete[] data;
Or, better, don't use new
and delete
at all, and use standard containers like std::vector<std::string>
. If you allocate more than one char
, then use delete[]
instead.
Upvotes: 3