I'm new to C++ and I have a problem with memory management.
I have one function a() that calls 3 functions (b(), c(), d()), each one of which returns a dynamically allocated array of MyClass objects:
void a(){
MyClass * one=b();
MyClass * two=c();
MyClass * three=d();
//operate with 3 array (one, two and three)
delete [] one;
delete [] two;
delete [] three;
}
MyClass * b(){
MyClass * array=new MyClass[2000];
//many operations on array
return array;
}
MyClass * c(){
MyClass * array=new MyClass[2000];
//many operations on array
return array;
}
MyClass * d(){
MyClass * array=new MyClass[2000];
//many operations on array
return array;
}
After many operations in a() I must delete the 3 arrays that I created with the 3 functions. If I do it using the 3 delete [] expressions like those in the code above, is it ok?
I asked myself this question because I think that this code deallocates everything correctly, but analyzing the memory allocation of my c++ program I see no evidence of this deletion.
new[]you should usedelete[]