返回局部变量 C++ 的指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19042552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning a pointer of a local variable C++
提问by user906357
I need to create a function that returns a pointer to an int.
我需要创建一个函数来返回一个指向 int 的指针。
Like so:
像这样:
int * count()
{
int myInt = 5;
int * const p = &myInt;
return p;
}
Since a pointer is simply an address, and the variable myInt is destroyed after this function is called. How do I declare an int inside this method that will keep a place in the memory in order for me to access it later via the returned pointer? I know I could declare the int globally outside of the function, but I want to declare it inside the function.
由于指针只是一个地址,因此在调用此函数后变量 myInt 将被销毁。如何在此方法中声明一个 int 以在内存中保留一个位置,以便我稍后通过返回的指针访问它?我知道我可以在函数外部全局声明 int,但我想在函数内部声明它。
Thanks in advance for any help!
在此先感谢您的帮助!
回答by Nathaniel Johnson
Use the new operator
使用新运算符
int * count()
{
int myInt = 5;
int * p = new int;
*p = myInt;
return p;
}
As pointed out in other answers this is generally a bad idea. If you must do it this way then maybe you can use a smart pointer. See this question for how to do this What is a smart pointer and when should I use one?
正如其他答案中所指出的,这通常是一个坏主意。如果您必须这样做,那么也许您可以使用智能指针。请参阅此问题以了解如何执行此操作 什么是智能指针以及何时应该使用?
回答by Ed Heal
You could use smart pointers.
你可以使用智能指针。
For example:
例如:
unique_ptr<int> count()
{
unique_ptr<int> value(new int(5));
return value;
}
Then you can access the integer as follows:
然后您可以按如下方式访问整数:
cout << "Value is " << *count() << endl;
回答by Jonathan Potter
You can do this by making the variable static:
您可以通过创建变量来做到这一点static:
int* count()
{
static int myInt = 5;
return &myInt;
}
回答by Anne Staley
It is an error to return a pointer to a local variable. x points to a variable allocated on the heap:
返回指向局部变量的指针是错误的。x 指向分配在堆上的变量:
link x = new node(a[m]);
Thus x isn't pointing to a local variable.
The reason that returning a pointer to a local variable is an error is that such a variable exists for only as long as the function is active (i.e. between it is entered and exited). Variables allocated on the heap (e.g. with the use of the new operator) exist until they are deallocated (e.g. with the delete operator).
返回指向局部变量的指针是错误的原因是这样的变量仅在函数处于活动状态时才存在(即在进入和退出之间)。在堆上分配的变量(例如使用 new 运算符)一直存在,直到它们被释放(例如使用 delete 运算符)。
回答by aaronman
If you want to return a pointer of a variable correctly you have to do something like.
如果你想正确返回一个变量的指针,你必须做类似的事情。
int * myInt = new int(5);
int * myInt = new int(5);
This is not a local variable BTW, meaning it does not have automatic storage and you have to deletethe memory yourself
这不是局部变量 BTW,这意味着它没有自动存储,您必须delete自己存储
However using pointers like this is generally unnecessary and unadvised. It's better to create an int outside the function and have the function take a reference.
然而,使用这样的指针通常是不必要的和不明智的。最好在函数外部创建一个 int 并让该函数引用。
void count(int & i)
{
i = 5;
}
BTW I don't know how you are planning to use the variable but since you also suggested using a global variable you may want to use a staticvar which @JonathanPotter suggested first. In many ways a static variable is similar to a global variable (both have static storage durations)
顺便说一句,我不知道您打算如何使用该变量,但由于您还建议使用全局变量,您可能希望使用static@JonathanPotter 首先建议的var。在许多方面,静态变量类似于全局变量(两者都有静态存储持续时间)

