C ++返回数组,数据丢失 [英] C++ Returning Array, data loss
问题描述
我是Java的C ++的新手,所以请原谅我太幼稚,但是我试图将数组从函数中传递出去.我知道这样做的方法是传递一个指针,如下所示:
I'm new to C++ coming from Java so forgive me for being naive, but I'm trying to pass an array out of a function. I know the way to do this is to pass a pointer, like so:
int *foo(){
int arr[3] = {1, 2, 3};
int *arrptr = arr;
return arrptr;
}
然后访问该数组:
int main(){
int *arrptr = foo(); //create a pointer to array
//cout all elements
for(int x = 0; x < 3; x++)
cout << arrptr[x];
return 0;
}
自然,这很好.但是由于某种原因,在不同的代码中,相同的过程仅返回数组的第一个值,而其余的似乎是随机的.
And naturally, this works just fine. But for some reason in a different code, this same process only returns the first value of the array and the rest seem to be random.
int main(){
stringstream allstats;
allstats << readFile(); //readFile() simply returns a string read from a file
while(!allstats.eof()){ //check for end of stream
string temp;
getline(allstats, temp); //grab a line from allstats and put it into temp
double *arr = calculateStats(temp); //create pointer to array of stats calculated for temp
// print arr
for(int x = 0; x < 6; x++)
cout << arr[x] << endl;
//****This spits out the correct value for arr[0] but for the rest of the array
//****it is values like 8.58079e-306 which tells me those addresses have been
//****overwritten.
}
return 0;
}
//calculate stats for each player, return as formatted string
double* calculateStats(string player){
//cut for conciseness, just know it works and the proper values are in the following array
//create stat array
double statarr[6] = {(double)BA, (double)OB, (double)H, (double)BB, (double)K, (double)HBP};
//create pointer to stat array
double *ptr;
ptr = statarr;
return ptr;
}
在Java中,就像返回一个数组一样简单,工作就完成了.在C ++中这是否可能?或者这超出了我的理解水平,并且解决方案比我想象的要复杂得多?
In Java it would be as simple as just returning an array and the job is done. Is this even possible in C++ or is this just beyond my level of understanding and the solution is much more complicated than I thought?
推荐答案
您不能在外部函数中具有指向本地数组的指针引用,因为一旦退出本地函数,它的堆栈就可能被重用,并且本地变量中的数据将包含垃圾内容
You cannot have pointer references to local arrays in external functions, since once the local function exits it's stack may be reused and the data in the local variables will contain trash.
您可以将static单词添加到本地数组声明中,以使其内存在其他函数(和重复调用)中保持不变.尽管这根本不是推荐的解决方案.
You can add word static to your local arrays declaration for it's memory to be persistent across other functions (and repeated calls). Although that is not a recommended solution at all here.
更改:
double statarr[6] = {(double)BA, (double)OB, (double)H, (double)BB, (double)K, (double)HBP};
收件人:
static double statarr[6] = {(double)BA, (double)OB, (double)H, (double)BB, (double)K, (double)HBP};
尽管,更好的解决方案是不将数组声明为 calculateStats
Although, a better solution would be to not declare the array local to calculateStats
类似的东西:
//calculate stats for each player, return as formatted string
double* calculateStats(string player, double *outArr){
//cut for conciseness, just know it works and the proper values are in the following array
//create stat array
outArr[0] = (double)BA;
outArr[1] = (double)OB;
outArr[2] = (double)H;
outArr[3] = (double)BB;
outArr[4] = (double)K;
outArr[5] = (double)HBP;
return outArr;
}
然后有:
int main(){
stringstream allstats;
allstats << readFile(); //readFile() simply returns a string read from a file
while(!allstats.eof()){ //check for end of stream
string temp;
getline(allstats, temp); //grab a line from allstats and put it into temp
double arr[6]
calculateStats(temp, arr) //create pointer to array of stats calculated for temp
// print arr
for(int x = 0; x < 6; x++)
cout << arr[x] << endl;
//****This spits out the correct value for arr[0] but for the rest of the array
//****it is values like 8.58079e-306 which tells me those addresses have been
//****overwritten.
}
return 0;
}
这篇关于C ++返回数组,数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!