简体   繁体   English

从函数返回指针

[英]Returning pointer from a function

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;
}   

Allocate memory before using the pointer. 在使用指针之前分配内存。 If you don't allocate memory *point = 12 is undefined behavior. 如果你没有分配内存*point = 12是未定义的行为。

int *fun()
{
    int *point = malloc(sizeof *point); /* Mandatory. */
    *point=12;  
    return point;
}

Also your printf is wrong. 你的printf也错了。 You need to dereference ( * ) the pointer. 您需要取消引用( * )指针。

printf("%d", *ptr);
             ^

Although returning a pointer to a local object is bad practice, it didn't cause the kaboom here. 虽然返回一个指向本地对象的指针是不好的做法,但它并没有在这里引起kaboom。 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. 如果该值未映射到有效的内存位置,您将获得SEGFAULT。 If by luck it mapped to something valid, then you just corrupted memory by overwriting that place with your assignment to 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在堆上分配内存。

It is not allocating memory at assignment of value 12 to integer pointer. 在将值12赋值给整数指针时不分配内存。 Therefore it crashes, because it's not finding any memory. 因此它崩溃了,因为它没有找到任何记忆。

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);
}

To my knowledge the use of the keyword new, does relatively the same thing as malloc(sizeof identifier). 据我所知,使用关键字new,与malloc(sizeof identifier)的功能相同。 The code below demonstrates how to use the keyword new. 下面的代码演示了如何使用关键字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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM