C++ Dynamic Memory Allocation
Posted: Sun Jan 09, 2011 5:45 pm
So I am new to using dynamic memory allocation commands in C++;
I have an array that holds a certain ClassType. I allocate memory with new for 10 positions of ClassType. At the end of the program I want to clean up the allocated memory so I delete all the ClassTypes in the array because I called them with new also. I want to do this but with a large class type.
But arr + i will not effectively move down the array will it because int is 2 bytes and my ClassTypes are bigger than 2 bytes.
Will this work ?
Also the array is part of a Class that acts like a stack and has private members of the array and the length which is also allocated with new.
I have an array that holds a certain ClassType. I allocate memory with new for 10 positions of ClassType. At the end of the program I want to clean up the allocated memory so I delete all the ClassTypes in the array because I called them with new also. I want to do this but with a large class type.
Code: Select all
for(int i=0;i<length;i--)
{
delete arr+i;
}
delete [] arr;
Will this work ?
Code: Select all
for(int i=0;i<length;i--)
{
delete *arr[i];
}
delete [] arr;