C语言 从函数返回指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7754986/
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 pointer from a function
提问by user567879
I am trying to return pointer from a function. But I am getting segmentation fault. Someone please tell what is wrong with the code
我正在尝试从函数返回指针。但是我遇到了分段错误。有人请告诉代码有什么问题
#include<stdio.h>
int *fun();
main()
{
int *ptr;
ptr=fun();
printf("%d",*ptr);
}
int *fun()
{
int *point;
*point=12;
return point;
}
回答by cnicutar
Allocate memory before using the pointer. If you don't allocate memory *point = 12is undefined behavior.
在使用指针之前分配内存。如果不分配内存*point = 12是未定义的行为。
int *fun()
{
int *point = malloc(sizeof *point); /* Mandatory. */
*point=12;
return point;
}
Also your printfis wrong. You need to dereference (*) the pointer.
还有你printf错了。您需要取消引用 ( *) 指针。
printf("%d", *ptr);
^
回答by invaliddata
Although returning a pointer to a local object is bad practice, it didn't cause the kaboom here. Here's why you got a segfault:
虽然返回一个指向本地对象的指针是不好的做法,但它并没有引起这里的混乱。这就是您遇到段错误的原因:
int *fun()
{
int *point;
*point=12; <<<<<< your program crashed here.
return point;
}
The local pointer goes out of scope, but the real issue is dereferencing a pointer that was never initialized. What is the value of point? Who knows. If the value did not map to a valid memory location, you will get a SEGFAULT. If by luck it mapped to something valid, then you just corrupted memory by overwriting that place with your assignment to 12.
本地指针超出范围,但真正的问题是取消引用从未初始化的指针。点的价值是什么?谁知道。如果该值未映射到有效的内存位置,您将获得一个 SEGFAULT。如果幸运它映射到有效的东西,那么你只是通过用你的分配为 12 覆盖那个地方来破坏内存。
Since the pointer returned was immediately used, in this case you could get away with returning a local pointer. However, it is bad practice because if that pointer was reused after another function call reused that memory in the stack, the behavior of the program would be undefined.
由于立即使用了返回的指针,在这种情况下,您可以通过返回本地指针来逃避。但是,这是不好的做法,因为如果在另一个函数调用重用堆栈中的内存之后重用该指针,则程序的行为将是未定义的。
int *fun()
{
int point;
point = 12;
return (&point);
}
or almost identically:
或几乎相同:
int *fun()
{
int point;
int *point_ptr;
point_ptr = &point;
*point_ptr = 12;
return (point_ptr);
}
Another bad practice but safer method would be to declare the integer value as a static variable, and it would then not be on the stack and would be safe from being used by another function:
另一个不好的做法但更安全的方法是将整数值声明为静态变量,然后它不会在堆栈中并且不会被另一个函数使用:
int *fun()
{
static int point;
int *point_ptr;
point_ptr = &point;
*point_ptr = 12;
return (point_ptr);
}
or
或者
int *fun()
{
static int point;
point = 12;
return (&point);
}
As others have mentioned, the "right" way to do this would be to allocate memory on the heap, via malloc.
正如其他人所提到的,这样做的“正确”方法是通过 malloc 在堆上分配内存。
回答by Varun Chhangani
It is not allocating memory at assignment of value 12 to integer pointer. Therefore it crashes, because it's not finding any memory.
它不会在将值 12 分配给整数指针时分配内存。因此它崩溃了,因为它没有找到任何内存。
You can try this:
你可以试试这个:
#include<stdio.h>
#include<stdlib.h>
int *fun();
int main()
{
int *ptr;
ptr=fun();
printf("\n\t\t%d\n",*ptr);
}
int *fun()
{
int ptr;
ptr=12;
return(&ptr);
}
回答by idleHands_94
To my knowledge the use of the keyword new, does relatively the same thing as malloc(sizeof identifier). The code below demonstrates how to use the keyword new.
据我所知,关键字 new 的使用与 malloc(sizeof identifier) 的作用大致相同。下面的代码演示了如何使用关键字 new。
void main(void){
int* test;
test = tester();
printf("%d",*test);
system("pause");
return;
}
int* tester(void){
int *retMe;
retMe = new int;//<----Here retMe is getting malloc for integer type
*retMe = 12;<---- Initializes retMe... Note * dereferences retMe
return retMe;
}

