我在删除使用下一个代码分配的内存时遇到了麻烦:
int main(){
char **cities;
cities = new char*[MAX_CITIES];
for(int i = 0; i < MAX_CITIES; i += 1){
cities[i] = new char[MAX_CITY_LENGTH];
cities[i] = "CITY"; //Its only a test value
}
cout << "Memory allocated successfully" << endl;
for(int i = 0; i < MAX_CITIES; i += 1){
cout << cities[i] << endl;
}
for(int i = 0; i < MAX_CITIES; i += 1){
delete[] cities[i];
}
delete[] cities;
cout << "Memory freed successfully" << endl;
return EXIT_SUCCESS;
}
我希望你能帮助我。谢谢。