My program starts with a dynamically allocated (DA) array. It then prompts the user to enter in a size. If the size entered is within a certain threshold, a new DA array is created, the contents of the old is copied into the new, and the new array is then displayed.
I am having trouble copying contents from one dynamically DA array into the other dynamically allocated array. Through each step of the reallocation process I have "print tests" that display the array after each process. I test the initialization and also the copying.
Please see the code below. Specifically if I enter 27, 28, 29 or 70 I get a bunch of weird numbers that look like memory addresses....and I can't figure out what I did wrong.
I cannot use vectors.
EDIT: omg THANK YOU so much for pointing my mistake out...was confusing the crap out of me. Thanks again everyone!!!
#include <iostream>
using namespace std;
int main () {
int maxSize = 25;
int active_size = 0;
int *uaptr;
uaptr = new int [maxSize];
for (int i=0; i<maxSize; i++)
*(uaptr + i) = 1;
cout << "\nWhat size you would like the array to be?: ";
cin >> active_size;
cin.clear();
cin.ignore (1000, 10);
if (active_size > (0.8 * maxSize)) {
maxSize *= 4;
int *tempPtr;
tempPtr = new int [maxSize];
for (int i=0; i<maxSize; i++)
*(tempPtr + i) = 0;
cout << "Testing initialization..." << endl;
for (int i=0; i<maxSize; i++) { //TEST INITIALIZATION
cout << *(tempPtr + i) << " ";
if ((i+1)%10==0)
cout << endl;
}
for (int i=0; i<active_size; i++) //Copy contents of old array into new,bigger array
*(tempPtr + i) = *(uaptr + i); //*****What is wrong here?!?!
cout << endl;
cout << "Testing the copying..." << endl;
for (int i=0; i<maxSize; i++) { //TEST COPYING -weird results when numbers 27, 28, 29 or 70 are entered
cout << *(tempPtr + i) << " ";
if ((i+1)%10==0)
cout << endl;
}
delete [] uaptr; //release old allocated memory
uaptr = tempPtr; //update the pointer to point to the newly allocated array
cout << endl;
for (int i = 0; i < active_size; i++) {
cout << *(uaptr + i) << " ";
if ((i + 1) % 10 == 0)
cout << endl;
}
}
}