-
memory and arrays
If I am creating a large array such as:
Code:
struct example
{
string word1, word2, word3, word4;
int num1, num2, num3;
};
example example1[100];
would I be better off leaving this as is or should I allocate memory from the heap for it and returning it when I am done such as:
Code:
example* example1 = new example[100];
//........
delete example1;
I think I have the syntax right?
R W Marsh
-
yes, since you normally have less stack space available than heap space. when using dynamic arrays remember that you must use 'delete [] example1' to ensure the destructor is called for each object in the array.
-
I would use a vector instead. This puts the memory on the heap, but manages the memory for you and calls the destructors automatically.