You can call delete[]
from anywhere. Your problems are much more prosaic. You just have a defect in your code.
You allocate an array of length size
. Then you increment size
. Then you do this:
for(int i = 0; i < size; ++i)
delete [] a[i];
And because you incremented size
already, your loop runs off the end of a
. You need to use the same value for size
as was used when you allocated the array.
To be quite clear, the following is the flow of execution:
size
is assigned to the value5
.a
is allocated with length5
.size
is incremented to the value10
.- You run a
for
loop from0
tosize-1
, that is0
to9
, and calldelete[] a[i]
.
Clearly the iterations 5
to 9
inclusive are accessing elements of a
that were not allocated. And that's undefined behaviour, and so on.
You could fix this by passing the new size to the realloc
function as a parameter. Something like this:
void realloc (char**& a, size_t newsize) {
for(int i = 0; i < size; ++i)
delete [] a[i];
delete [] a;
size = newsize;
a = new char* [size];
for (int i = 0; i < size; ++i)
a[i] = new char [size];
}
Obviously you would not modify size
outside of this function. You would call the function like this:
realloc(a, size+5);
Taking it a bit further, you might choose to handle the allocations like this:
size_t size = 5;
void alloc(char**& a, size_t newsize) {
size = newsize;
a = new char*[size];
for (int i = 0; i < size; ++i)
a[i] = new char[size];
}
void realloc(char**& a, size_t newsize) {
for(int i = 0; i < size; ++i)
delete[] a[i];
delete[] a;
alloc(a, size+5);
}
All that said, and being perfectly frank, your entire program is a disaster in the making. There are many other errors that I've not covered in this answer. Use standard containers like std::vector
and std::string
. The standard containers will handle the details of memory allocation, and will do it correctly.