开发者

Array not returned correctly

I am trying to return a simple array, but I am not getting the correct result. I am getting the following

arr1[0] = 1 
arr1[1] = 32767

result while the result should have been

  arr1[0] = 1 
  arr1[1] = 15

Please suggest.

int *sum(int a, int b){
  int arr[2];
  int *a1开发者_C百科;
  int result = a+b;
  arr[0]= 1;
  arr[1]= result;
  a1 = arr;
  return a1;
}
int main(){  

  int *arr1 =  sum(5,10);
  cout<<"arr1[0] = "<<arr1[0]<<endl;
  cout<<"arr1[1] = "<<arr1[1]<<endl;

  return 0;

}


You can't return arrays. All you are returning is a pointer to a local object which is destroyed when it goes out of scope as the function returns.

You can pass in a pointer to the array, so the array is modified by the function.

Also, you can return a copy of an array if it is wrapped inside a struct/class, such as std::tr1::array (or boost::array).


Try this...


#include <vector>
#include <iostream>

std::vector<int> sum(int a, int b)
{ 
  std::vector<int> rv(2);
  rv[0]= 1; 
  rv[1]= a+b; 
  return rv; 
} 

int main()
{   
  std::vector<int> arr1 = sum(5,10); 
  std::cout << "arr1[0] = " << arr1[0] << std::endl; 
  std::cout << "arr1[1] = " << arr1[1] << std::endl; 

  return 0; 
} 


You are returning a pointer to a local variable - when sum() has returned to main() the array isn't around anymore.

You can't even return arrays by value unless you wrap them, but you could simply return a standard container like std::vector or a std::pair:

std::pair<int,int> f(int a, int b) {
    return std::make_pair(1, a+b);
}

int main() {
    std::pair<int,int> res = f(5,10);
    std::cout << res.first << ", " << res.second << std::endl;
}


a1 is a local variable, allocated memory on stack. If you want to return int *, you should allocate memory on the heap.

int *arr = (int *)malloc(sizeof(int) * 2));

//CAUTION you need to de-allocate memory [read about free()]

Explanation:

See arr is actually an array as long as the function int *sum(int, int) does not complete. SO, what you return is actually the address where arr actually was, and is no longer there.

When, you try to access that memory location in main(), you just get garbage/random values.


it is very wrong. int arr[2]; is local variable and lives in stack frame. you can't return pointers to stack from function. they become invalid.


Hmm.. well, arr is a stack variable and you're returning a pointer to it and expecting it to still be valid.


It is wrong to return a pointer to a locally defined array. a1 is placed on the stack, which gets cleaned when the function returns. Therefore you are no longer allowed to access that memory.

To "return an array" you can dynamically allocate it with new and then return it. Keep in mind that this will force you to free it later, so you'll have to carefully design the memory management policy for that array.


arr is on the stack - once you return from sum(), it no longer exists. A pointer to it might return random data, which is what you're seeing when you return a1.


You should allocate a memory for the arr first, so it is not a stack variable. Then somebody outside your function should free it. It is better if you pass arr as a parameter to the function, so you can allocate and deallocate memory for it in one and the same abstraction level.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜