I'm having some issues assigning values to a dynamically allocated array immediately after a row of the array has been created. When printing the output of the array inside the "creation loop" (after the assignment of a value to the newly created array element), the output is correct but when printing outside the "creation loop", the output is garbage. Is there something in the standards of C or C++ that says you can't do something like this?
Here's some example code that illustrates this problem:
Ouch! You are allocating just enough memory for one int, not an array of int's. Then you are storing values into unallocated memory. Then you reallocating memory without deallocation the old memory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int *exampleArray;
cout << "Array output inside creation loop..." << endl;
exampleArray = newint[10]; //allocate enough memory for 10 int's
for(int i = 0; i < 10; i++)
{
// exampleArray = new int;
exampleArray[i] = i;
cout << exampleArray[i] << endl;
}
cout << endl << "Array output outside creation loop..." << endl;
for(int i = 0; i < 10; i++)
cout << exampleArray[i] << endl;
delete exampleArray;