I can create a dynamic 2d-array of 3x2 ints and I can delete it without problems. But when doing the same with a 2d-array of strings, deleting it generates the error:
munmap_chunk(): invalid pointer
Why? This lack of homogeneity between ints and strings is preventing me from writing a template that can be instantiated with strings.
I know there are automatic pointers. I know there are better alternatives to primitive language arrays. But I am a teacher and I am trying to introduce the subjects one by one, so I still cannot use those more advanced topics. I am trying to explain abstract types of data with templates.
#include<string>
#include<iostream>
int main()
{
std::cout << "2d-ARRAY of ints" << std::endl;
int **a = new int*[3];
for(int i=0; i<3; i++)
a[i] = new int[2];
for(int i=0; i<3; i++)
delete a[i];
delete [] a;
std::cout << "2d-ARRAY of strings" << std::endl;
std::string **s = new std::string*[3];
for(int i=0; i<3; i++)
s[i] = new std::string[2];
for(int i=0; i<3; i++)
delete s[i];
delete [] s;
return 0;
}