连续输出数组的值会产生垃圾。 [英] Outputting values of an array consecutively produces garbage.

查看:100
本文介绍了连续输出数组的值会产生垃圾。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回指向数组的指针的函数。我在main()中调用它并检索指针。当我尝试循环访问值时,只能访问一个值而不将其余值转换为垃圾。我只能访问一个索引,如果我尝试超过2个,其中一个转为垃圾。



I have a function that return a pointer to an array. I call this in main() and retrives the pointer. The when i try to loop over to access the values, only one value can be accessed without the rest being turned into garbage. I can only access one index, if i try more than 2, one of them turns to garbage.

//This does not work, gives garbage
int *p = generateArray(10);

for(int i =0;i <10;i++){
cout << p[i];
}

cout <<p[i]; // works for any index, but if copy paste this at the next line, it gives garbage.





这是完整的代码。





Here is the complete code.

#include <iostream>
#include <string>
#include <ctime>
#include <iterator>
#include <cstdlib>


using namespace std;

int randomNum(int lowerBound, int upperBound){
    int random;
    random = lowerBound + rand() % (upperBound - lowerBound);
    return random;
}



int checkDuplicate(int size,int value, int *pholdArray){
    int *arrholder = pholdArray;
    for(int g =0;g<size;g++){
        if(arrholder[g] == value){
            return 3;
        }
    }
    return 4;
}


/*
int* generateArray(int size){
    int *parray = new int[size];
    return parray;
}
*/


int* fillArray(int size, int lowerBound, int upperBound){
    int counter_repeat;
    int loopcount = 0;
    int repeat =0;
    int random;
    int receive;
    int setrand;
    int pholdArray[size];

    if(checkDuplicate(size,random,pholdArray) == 3);
        counter_repeat++;
        for(int i =0;i<size;i++){
            random = randomNum(lowerBound,upperBound);
            if(checkDuplicate(size,random,pholdArray) == 3){
                counter_repeat++;
            while(checkDuplicate(size,random,pholdArray) == 3){
                random = randomNum(lowerBound,upperBound);
                counter_repeat++;
            }
        }
            pholdArray[i] = random;

        if(i == size-1){
            int *finalArray = pholdArray;
        return finalArray;
        }
    }
        //int *finalArray = pholdArray;
        //return finalArray;
}



int main(){

    srand (time(0));
   // int *p = generateArray(10);
    int *n = fillArray(10,0,10);
    cout << n[1]; // works
    cout << n[2]; // garbage

    for(int d = 0;d<10;d++){
    cout << n[6] << "\n"; // garbage 
    }
return 0;
}

推荐答案

在fillArray()中你已经在堆栈堆上创建了一个本地数组(自动变量),假装结果归还。这是错误当函数返回时,堆栈被下一个函数重用,而填充了它们的自动数据,这对你来说是垃圾!

如果你需要一个数组来回馈用new运算符创建它,或者从malloc中分配它。

其他观察:处理编译器警告,你应该有一些,因为你的函数不是所有的路径都返回值!

这是fillArray()的正确编码。 完成数组后不要忘记释放内存(使用删除)。

In fillArray() you have created a local array (automatic variable) on the stack heap, pretending to give it back as result. This is wrong when the function returns the stack is reused by next functions and filled with their automatic data that for you are garbage!
If you need an array to give back create it with the new operator, or allocate it from malloc.
Other observation: take care of compiler warnings, you should have had some, because not all paths of your function return a value!
This is the correct coding for fillArray(). And don't forget to free memory (using Delete) when finished with the array.
int* fillArray(int size, int lowerBound, int upperBound){
    int counter_repeat;
    int loopcount = 0;
    int repeat =0;
    int random;
    int receive;
    int setrand;
    int *pholdArray= new int[size];    //allocate memory for the array
 
    if(checkDuplicate(size,random,pholdArray) == 3);
        counter_repeat++;
        for(int i =0;i<size;i++){>
            random = randomNum(lowerBound,upperBound);
            if(checkDuplicate(size,random,pholdArray) == 3){
                counter_repeat++;
            while(checkDuplicate(size,random,pholdArray) == 3){
                random = randomNum(lowerBound,upperBound);
                counter_repeat++;
            }
        }
            pholdArray[i] = random;
 
        if(i == size-1){
            return pholdArray;
        }
    }
    return pholdArray;
}


C ++代码可以从标准库使用中获得很大的好处。如果你只想用随机数填充数组而不重复,那么你可以写:

C++ code can get great benefits from standard library usage. If you just want to fill the array with random numbers without repetition then you could write:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 20;
const int LB = 0;
const int UB = 19;
int main()
{
  vector <int> v;

  for (int n=LB; n<=UB; ++n)
    v.push_back(n);

  random_shuffle(v.begin(), v.end());


  for (size_t n=0; n<v.size(); ++n)
    cout << v[n] << endl;
}


这篇关于连续输出数组的值会产生垃圾。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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