1
我有以下代碼。爲什麼創建一個字符串會影響其他變量?
int *x = somefuction();//return array of two elements
string s;
cout << x[0] << " and " << x[1];
該代碼打印出意外的值。但是如果我評論「字符串」;排它工作正常。是什麼原因?
一些功能是:
int* getRowCol(int l){
int min = floor(sqrt(l));
int max = ceil(sqrt(l));
int area = 100000;
int result[2];
for (int col = min; col <= max; col += 1){
for (int row = min; row <= col; row += 1){
if (col*row < area && row*col>=l){
area = row*col;
result[0] = row;
result[1] = col;
}
}
}
return result;
}
'somefuction()'返回其局部變量的地址? –
未定義的行爲。 – dasblinkenlight
將somefunction()添加到問題 – chamathabeysinghe