Is the following ok?
int n=5;
map<string,int> * maps = (map<string,int> *)malloc(n*sizeof(map<string,int>));
for (int i=0; i<n; i++) {
maps[i] = map<string,int>();
char * i_str = (char *)malloc(10);
sprintf(i_str,"%d",i);
char * key = (char *)malloc(100);
strcpy(key,"key");
strcat(key,i_str);
(maps[i])[string(key)] = i*i;
}
I know people say to use new rather than malloc in C++. But if I do it this way, what if any problems can occur?
Edit: Code compiles fine and runs fine (g++ 4.6.3). Not sure why the question is on hold, but I just want to know if it's technically correct (I don't care about style). One thing I'm not sure is whether the line
maps[i] = map<string,int>();
is syntactically correct, and whether it really does lead to unexpected behaviour as one person suggested.
Someone has yet to provide a reference to the C++ specification that shows that what I'm doing is undefined. So this question is still not answered.