如何在 C++ 中返回本地数组?
char *recvmsg(){
char buffer[1024];
return buffer;
}
int main(){
char *reply = recvmsg();
.....
}
我收到警告:
警告 C4172:返回局部变量或临时变量的地址
char *recvmsg(){
char buffer[1024];
return buffer;
}
int main(){
char *reply = recvmsg();
.....
}
I get a warning:
warning C4172: returning address of local variable or temporary
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我建议
std::vector
:然后,如果您在代码中需要
char*
,那么您可以使用&reply[0]
任何时候你想要的。例如,你就完成了。这意味着,如果您使用 C API,那么您仍然可以使用
std::vector
,因为您可以将&reply[0]
传递给 C API (如上所示),并回复
C++ API。底线是:尽可能避免使用
new
。如果您使用new
,那么您必须自己管理它,并且在不需要时delete
。I would suggest
std::vector<char>
:And then if you ever need
char*
in your code, then you can use&reply[0]
anytime you want. For example,And you're done. That means, if you're using C API, then you can still use
std::vector
, as you can pass&reply[0]
to the C API (as shown above), andreply
to C++ API.The bottomline is : avoid using
new
as much as possible. If you usenew
, then you've to manage it yourself, and you've todelete
when you don't need it.您需要动态分配 char 数组:
对于 C++ 和
C。
发生的情况是,如果没有动态分配,您的变量将驻留在函数的堆栈上,因此在退出时将被销毁。这就是您收到警告的原因。在堆上分配它可以防止这种情况发生,但是您必须小心,并在通过
delete[]
使用完成后释放内存。You need to dynamically allocate your char array:
for C++ and
for C.
What happens is, without dynamic allocation, your variable will reside on the function's stack and will therefore be destroyed on exit. That's why you get the warning. Allocating it on the heap prevents this, but you will have to be careful and free the memory once done with it via
delete[]
.警告消息是正确的。您返回的是本地数组的地址,该地址在函数返回后消失。
您可以使用动态内存分配来做到这一点:
问题是您需要确保稍后
free()
指针以避免内存泄漏。或者,您可以将缓冲区传递到函数中。
这避免了内存分配的需要。这实际上是首选方法。
The warning message is correct. You're returning the address of a local array which disappears after the function returns.
You can do this using dynamic memory allocation:
The catch is that you need to make sure you
free()
the pointer later on to avoid a memory leak.Alternatively, you can pass the buffer into the function.
This avoids the need for a memory allocation. This is actually the preferred way to do it.
问题是
buffer
存在于堆栈中,并在退出recvmsg
时超出范围。您可以在堆上分配缓冲区:
请注意,现在调用者负责处置分配的内存:
The problem is that
buffer
lives on the stack and goes out of scope the moment you exitrecvmsg
.You could allocate
buffer
on the heap:Note that now the caller is responsibe for disposing of the allocated memory:
您有几个选择...您现在执行的方式将导致未定义的行为,因为一旦函数返回,数组将超出范围。所以一种选择是动态分配内存。
只要记住用这种方式删除它(或者如果使用 malloc 则释放)来清理它。其次,您可以使用参数...
同样,这里的清理与之前的清理是一样的。另请注意对 0 的检查,以确保不会对已分配的指针调用 new。
最后,你可以使用向量..
You have a few options...The way you're doing it now is going to cause undefined behavior as the array will go out of scope once hte function returns. So one option is to dynamically allocate the memory..
Just remember to clean it up with delete this way (or free if you used malloc). Second, you could use a parameter...
Again, the same thing goes for clean up here as with the previous. Also note the check for 0 to make sure you don't call new on a pointer that's been allocated already.
Last, you could use a vector..
只是为了完成图片:
没有必要使用 malloc 分配内存。您还可以在堆栈上创建缓冲区,但必须在堆栈帧上创建缓冲区,该堆栈帧的生存时间与缓冲区的使用者一样长。这是OP的错误——当被调用者完成时,缓冲区被删除,调用者得到一个无效的指针。
所以你可以做的是:
Just to complete the picture:
It is not necessary, to allocate memory with malloc. You can also create the buffer on the stack, but you must create it on a stack frame that lives as long as the consumer of the buffer lives. That was the error of the OP -- when the callee was finished, the buffer was deleted and the caller got a invalid pointer.
So what you can do is this:
您可以动态创建缓冲区,但调用者需要知道如何释放它。
我认为最好传入一个缓冲区(假设recvmsg也填充它)
即使调用者决定动态更好,他们也会知道他们需要释放它,以及执行此操作的具体方法(free(),delete,delete [],或者可能是自定义分配器中的特殊内容)
You could dynamically create the buffer, but then the caller needs to know to free it.
I think it's better to pass in a buffer (assuming recvmsg also fills it)
Even if the caller decides dynamic is better, they will know that they need to free it, and the specific way to do that (free(), delete, delete[], or perhaps something special from a custom allocator)
问题是您正在返回指向堆栈上分配的缓冲区的指针。一旦函数返回,该缓冲区就不再有效。
The problem is that you are returning a pointer to a buffer allocated on the stack. Once the function returns, that buffer is no longer valid.
您正在
recvmsg
函数内部的堆栈上分配数组。如果取消引用该内存,则返回指向该内存的指针有时会导致未定义的行为,因为函数退出时内存将被清理。如果您想返回指向内存的指针,则需要使用
malloc
动态分配它。You are allocating the array on the stack inside of your
recvmsg
function. Returning a pointer to that memory will at some point lead to undefined behavior if it is dereferenced as the memory will be cleaned up when the function exits.If you want to return a pointer to memory you will need to allocate it dynamically using
malloc
.当您返回缓冲区时,因为它充当指向数组第一个位置的指针,因此它将返回其地址。在您调用该函数的地方,您可以创建一个字符指针来存储这个返回的地址值之后您可以移动指针并访问数组的所有元素。
when you are returning the buffer then as it acting as a pointer to the first location of the array so it will return its address.And the place where you are calling the function there you can make a character pointer which will store this returned address value .After which you can move your pointer and can access all the elements of your array.
通过 ref 怎么样
how about passing by ref