麻烦复制数组的内容到另一个数组......越来越怪异号码 [英] Trouble copying contents of an array into another array...getting weird numbers

查看:160
本文介绍了麻烦复制数组的内容到另一个数组......越来越怪异号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序与动态分配(DA)阵列开始。然后,它提示用户的尺寸输入。如果输入的大小是一定的阈值范围内,一个新的DA数组被创建,旧的内容被复制到新的,并随后显示在新的数组

我有从一个动态的达阵麻烦复制内容到其他动态分配的数组。通过重新分配过程的每个步骤我有打印测试是显示每个处理后的阵列。我测试的初始化,也复制。

请参阅下面的code。具体来说,如果我输入27,28,29或70,我得到了一堆看起来像内存地址奇异数的....我想不出我做错了什么。

我不能使用的载体。

编辑:OMG非常感谢你的指点我的错误了......令人困惑的废话了我。再次感谢大家!

 的#include<&iostream的GT;
使用命名空间std;诠释主(){INT MAXSIZE = 25;
INT active_size = 0;
INT * uaptr;
uaptr =新INT [MAXSIZE];
的for(int i = 0; I< MAXSIZE;我++)
    *(uaptr + I)= 1;COUT<< \\ nWhat大小,你想数组是?:;
CIN>> active_size;
cin.clear();
cin.ignore(1000,10);
如果(active_size>(0.8 * MAXSIZE)){
    MAXSIZE * = 4;    INT * tempPtr;
    tempPtr =新INT [MAXSIZE];    的for(int i = 0; I< MAXSIZE;我++)
        *(tempPtr + I)= 0;    COUT<< 测试初始化​​......<< ENDL;
    的for(int i = 0; I< MAXSIZE;我++){//测试初始化
        COUT<< *(tempPtr + I)&所述;&下; ;
        如果(第(i + 1)%10 == 0)
            COUT<< ENDL;
    }    的for(int i = 0; I< active_size;我++)//旧数组到新的,更大的阵列的复制内容
        *(tempPtr + I)= *(uaptr + I); // *****什么是错在这里?!?!    COUT<< ENDL;
    COUT<< 测试复制......<< ENDL;
    的for(int i = 0; I< MAXSIZE;我++){//当数27,28,29或70进入测试复印-weird结果
        COUT<< *(tempPtr + I)&所述;&下; ;
        如果(第(i + 1)%10 == 0)
            COUT<< ENDL;
    }        删除[] uaptr; //释放旧的内存分配
        uaptr = tempPtr; //更新指针指向新分配的数组    COUT<< ENDL;
    的for(int i = 0; I< active_size;我++){
        COUT<< *(uaptr + I)&所述;&下; ;
        如果(第(i + 1)%10 == 0)
            COUT<< ENDL;
        }
    }}


解决方案

您的循环虽然新的,更大的数组大小的所有道路。但旧阵列并不大。所以,你当你走过去的原始数组的大小得到一堆乱七八糟的值。看看这里:

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

您一路循环新的大小年底, active_size 。但 uaptr 只有大小 MAXSIZE 处于起步,这是25所以,你的覆盖人数近25时, ,你就开始从......谁知道?

获取数据

您复制循环仅应去原数组或25在这种情况下的大小。你不认为大小存储在任何地方,所以你需要保存它是一个变量。 old_size 。然后循环只是远远复制时:

 的for(int i = 0; I< old_size;我++)
    *(tempPtr + I)= *(uaptr + I);

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

}

解决方案

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

这篇关于麻烦复制数组的内容到另一个数组......越来越怪异号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆