I'm trying to return a pointer to an array from a function but I have an issue. When I try to output like this:
我试图从函数返回指向数组的指针,但我有一个问题。当我尝试像这样输出:
#include <iostream>
using namespace std;
int* Somma_Array(int[],int[],int);
int main()
{
int n;
cin>>n;
int A[n],B[n];
for(int i=0;i<n;i++)cin>>A[i];
for(int i=0;i<n;i++)cin>>B[i];
int *c=Somma_Array(A,B,n);
for(int i=0;i<n*2;i++)cout<<c[i];
}
int* Somma_Array(int v[],int p[],int size)
{
int r[size*2];
for(int i=0;i<size;i++)r[i]=v[i];
for(int i=0;i<size;i++)r[i+size]=p[i];
return r;
}
it prints weird numbers instead of the actual number. I tried to do what this question says but it does not work. It gives me the following warning:
它打印奇怪的数字而不是实际的数字。我试着做这个问题所说的但它不起作用。它给了我以下警告:
[Warning] address of local variable 'r' returned [enabled by default]
I'm using bloodshed dev-c++.
我正在使用流血的dev-c ++。
3 个解决方案
#1
3
The following:
int r[size*2];
defines r
locally. When the function exits (as in the scope of the function expires), r
will be destroyed since it is bound to the function's scope. You are likely seeing junk data from the stack frame.
在本地定义r。当函数退出时(如函数的范围到期),r将被销毁,因为它绑定到函数的作用域。您可能会看到堆栈帧中的垃圾数据。
you could fix this by doing the following:
您可以通过执行以下操作来解决此问题:
int* r = new int[size * 2];
The variable r
will now be heap allocated and exist beyond the scope of the function.
变量r现在将被堆分配并存在于函数范围之外。
IMPORTANT by doing this, you now must manually free r
when you are done with it. So for instance, your calling code will look something like this:
重要的是,完成此操作后,您现在必须在完成后手动释放r。例如,您的调用代码将如下所示:
int* result = Somma_Array(v, p, size);
/* ... do stuff ... */
delete[] result;
Since r
is an array, note the use of delete[]
instead of delete
. delete[]
is the correct way to destroy arrays.
由于r是一个数组,请注意使用delete []而不是delete。 delete []是销毁数组的正确方法。
A Better Alternative
一个更好的选择
Would std::vector be more what you are after? This is a much safer alternative to hand-rolled arrays. The vector is safer to use, scales automatically as you add elements, and cleans itself up nicely when it leaves scope (assuming you are using a value-type instance). Additionally, vectors can be copied and moved out of functions easily.
std :: vector会更像你追求的吗?这是手动排列的更安全的替代方案。向量使用起来更安全,在添加元素时自动缩放,并在离开作用域时自行清理(假设您使用的是值类型实例)。此外,矢量可以轻松复制并移出功能。
#2
2
You define a stack allocated array r
, which is destroyed when you exit the function Soma_Array
. This is one of the (many) reasons vectors are preferred to plain arrays - they handle allocation and deallocation for you.
您定义了一个堆栈已分配的数组r,当您退出函数Soma_Array时会将其销毁。这是(很多)原因之一,矢量比普通数组更受欢迎 - 它们为您处理分配和释放。
#include <vector>
std::vector<int> getArray()
{
std::vector<int> a = {1, 2, 3};
return a;
}
#3
1
You cannot return arrays in C++. Especially, you should not return a pointer to a local array. You can however return a std::vector<int>
:
您无法在C ++中返回数组。特别是,您不应该返回指向本地数组的指针。但是你可以返回一个std :: vector
std::vector<int> Somma_Array(int v[], int p[], int size)
{
std::vector<int> r(2 * size);
std::copy(v, v + size, r.begin());
std::copy(p, p + size, r.begin() + size);
return r;
}
#1
3
The following:
int r[size*2];
defines r
locally. When the function exits (as in the scope of the function expires), r
will be destroyed since it is bound to the function's scope. You are likely seeing junk data from the stack frame.
在本地定义r。当函数退出时(如函数的范围到期),r将被销毁,因为它绑定到函数的作用域。您可能会看到堆栈帧中的垃圾数据。
you could fix this by doing the following:
您可以通过执行以下操作来解决此问题:
int* r = new int[size * 2];
The variable r
will now be heap allocated and exist beyond the scope of the function.
变量r现在将被堆分配并存在于函数范围之外。
IMPORTANT by doing this, you now must manually free r
when you are done with it. So for instance, your calling code will look something like this:
重要的是,完成此操作后,您现在必须在完成后手动释放r。例如,您的调用代码将如下所示:
int* result = Somma_Array(v, p, size);
/* ... do stuff ... */
delete[] result;
Since r
is an array, note the use of delete[]
instead of delete
. delete[]
is the correct way to destroy arrays.
由于r是一个数组,请注意使用delete []而不是delete。 delete []是销毁数组的正确方法。
A Better Alternative
一个更好的选择
Would std::vector be more what you are after? This is a much safer alternative to hand-rolled arrays. The vector is safer to use, scales automatically as you add elements, and cleans itself up nicely when it leaves scope (assuming you are using a value-type instance). Additionally, vectors can be copied and moved out of functions easily.
std :: vector会更像你追求的吗?这是手动排列的更安全的替代方案。向量使用起来更安全,在添加元素时自动缩放,并在离开作用域时自行清理(假设您使用的是值类型实例)。此外,矢量可以轻松复制并移出功能。
#2
2
You define a stack allocated array r
, which is destroyed when you exit the function Soma_Array
. This is one of the (many) reasons vectors are preferred to plain arrays - they handle allocation and deallocation for you.
您定义了一个堆栈已分配的数组r,当您退出函数Soma_Array时会将其销毁。这是(很多)原因之一,矢量比普通数组更受欢迎 - 它们为您处理分配和释放。
#include <vector>
std::vector<int> getArray()
{
std::vector<int> a = {1, 2, 3};
return a;
}
#3
1
You cannot return arrays in C++. Especially, you should not return a pointer to a local array. You can however return a std::vector<int>
:
您无法在C ++中返回数组。特别是,您不应该返回指向本地数组的指针。但是你可以返回一个std :: vector
std::vector<int> Somma_Array(int v[], int p[], int size)
{
std::vector<int> r(2 * size);
std::copy(v, v + size, r.begin());
std::copy(p, p + size, r.begin() + size);
return r;
}