Trouble copying contents of an array into another

2019-08-11 03:29发布

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;
        }
    }

}

3条回答
太酷不给撩
2楼-- · 2019-08-11 04:03

Your problem is likely undefined behaviour because active_size is read in from user input but has no relation to the actual size of your arrays. Therefore here

for (int i=0; i<active_size; i++)  //Copy contents of old array into new,bigger array
    *(tempPtr + i) = *(uaptr + i); 

because you are using it as an index limit, you could quite easily be reading and writing beyond the bounds of one or both arrays.

Your size of tempPtr is specified here

if (active_size > (0.8 * maxSize)) {                      
    maxSize *= 4;                                                 

    int *tempPtr;                                                 
    tempPtr = new int [maxSize];                         

which bears no relation to active_size . Thus if you ever enter active_size as larger than 25 (the size of your uaptr array) you are reading memory beyond the limit of uaptr which is undefined behaviour.

查看更多
干净又极端
3楼-- · 2019-08-11 04:14

You are looping all the way though the new, bigger array size. But the old array isn't that big. So you are getting a bunch of garbage values when you go past the size of the original array. Look here:

for (int i=0; i<active_size; i++) 
    *(tempPtr + i) = *(uaptr + i);

You are looping all the way to the end of the new size, active_size. But uaptr is only the size that maxSize was at the start, which is 25. So when your reach numbers past 25, you start pulling data from... who knows?

Your copy loop should only go to the size of the original array, or 25 in this case. You don't store that size anywhere, so you need to store it is a variable. old_size. And then loop only that far when copying:

for (int i=0; i<old_size; i++) 
    *(tempPtr + i) = *(uaptr + i);
查看更多
We Are One
4楼-- · 2019-08-11 04:20

Your code contains logical errors. For example initially active_size id equal to 0 though you already allocated an error of maxSize elements that is 25 elements. When you enter a new value in actual_size that can be greater than max_size. And after that you use this new value of actual_size that to copy elements from the old array into the new array, But old array can have less elements than the entered value of actual_size. So this statement

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?!?!

has undefined behaviour. It can try to access memory beyond the old array.

You should use some variable that will store the number of elements of the current array. Also you can initialize a new allocated array with zeroes along with using opertaor new. For example

tempPtr = new int [maxSize] {};

or

tempPtr = new int [maxSize]();

Also you can use standard algorithms for these operations. For example let assume that the size of the current array is old_length. Then you can write

std::fill( std::copy( uaptr, uaptr + old_length, tempPtr ), tempPtr + maxSize, 0 );
查看更多
登录 后发表回答