指针作为函数返回并使用malloc

The*_*war 3 c free pointers function-pointers

我使用指针作为函数返回..下面是一段简单的代码

主功能:

void main()
{
    int a = 10, b = 20;
    int *ptr;
    ptr = add(&a, &b);
    printf("sum of a and b is %d\n", *ptr);
}
Run Code Online (Sandbox Code Playgroud)

添加功能:

int* add(int *a, int *b)
{
    int c;
    c = *(a)+*(b);
    return &c;
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,给我输出30 ..

但是如果你printhelloworld();在下面添加之前再添加一个函数

void main()
{
    int a = 10, b = 20;
    int *ptr;
    ptr = add(&a, &b);

    printhelloworld();--this just prints hello world

    printf("sum of a and b is %d\n", *ptr);
}
Run Code Online (Sandbox Code Playgroud)

由于堆栈帧被释放,输出将不再是30并且它是未定义的.所以我必须修改我的程序,如下所示 malloc()

int* add(int *a, int *b)
{    
    int* c = (int*)malloc(sizeof(int));
    *c = *(a)+*(b);
    return c;
}
Run Code Online (Sandbox Code Playgroud)

这有效.

但是如果我不释放堆中分配的内存,它会不会永远存在?我不应该free()像下面这样使用吗?

free(c);
Run Code Online (Sandbox Code Playgroud)

如果我free()在main中使用,c不在范围内它将无法工作,如果我在添加中使用'free`,我将再次得到未定义的结果.

问:在我的案例中
使用的正确方法是什么free()

free(C);
Run Code Online (Sandbox Code Playgroud)

repro的总计划

#include<stdio.h>
#include<stdlib.h>

void printhelloworld()
{

    printf("hello world\n");
}

int* add(int *a, int *b)
{

    int* c = (int*)malloc(sizeof(int));

    *c = *(a)+*(b);
    //free(c);
    return c;
}


int main()
{

    int a = 10, b = 20;

    int *ptr;
    ptr =(int*) malloc(sizeof(int));
    ptr = add(&a, &b);

printhelloworld();
printf("sum of a and b is %d\n", *ptr);

}
Run Code Online (Sandbox Code Playgroud)

Sto*_*ica 5

我假设它int是一种更复杂类型的替代品.

可以动态地分配内存,然后找出必须释放它的兔子洞.

或者,您可以让调用者add决定应该在何处分配新对象.您可以接受指向对象的指针,add然后写入:

void add(int *a, int *b, int *c)
{    
    *c = *(a)+*(b);
}
Run Code Online (Sandbox Code Playgroud)

现在调用代码可以轻松决定使用自动变量:

int c;
add(&a, &b, &c);
Run Code Online (Sandbox Code Playgroud)

如果不需要动态分配,请不要担心.由于减少了责任,因此更容易维护代码IMO add.


Gil*_*il' 5

int* add(int *a, int *b)
{
    int c;
    c = *(a)+*(b);
    return &c;
}
Run Code Online (Sandbox Code Playgroud)

这工作正常,给我输出30 ..

这不起作用,它只是在一个简单的情况下出现:c是一个局部变量,因此在函数返回时释放存储加法结果的内存.变量使用的内存不会被立即擦除或重用,因此当您在add返回后尝试访问它时,您将获得您期望的值.但是,当您调用另一个函数时printhelloworld,它会将相同的内存用于其自己的局部变量,并且在某些时候内存的内容会更新.

请注意,即使在简单的情况下,也无法保证您会看到结果.指针使用强化工具或编译器优化可能导致程序崩溃,显示不同的值,或者在此时以令人困惑的方式表现.C称这种"未定义的行为".

如果要在函数中分配一些内存并返回指向它的指针,则调用malloc该函数是正确的.分配的内存malloc在您呼叫之前一直有效free.既然你需要调用free您已使用内存结束后,这有发生后,add回报:你确实需要调用freemain.能够调用mallocfree从不同的功能是动态分配内存的整点.

您需要传递的freemalloc返回的指针值.您已将此值分配给c,然后c从中返回add,因此您需要传递的地址free是返回的值add.变量ptrmain包含相同的值作为变量cfree.您可以使用它来访问存储在此地址的值,您也可以使用它来释放内存块.

int* add(int *a, int *b)
{    
    int* c = malloc(sizeof(int));  // no need for a cast here
    printf("The pointer is %p\n", (void*)c);
    if (c == NULL) { // Abort the program if the allocation failed
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    *c = *(a)+*(b);
    return c;
}

int main()
{
    int a = 10, b = 20;
    int *ptr;
    ptr = add(&a, &b);
    printf("The pointer is still %p\n", (void*)ptr);
    // ...
    printf("sum of a and b is %d\n", *ptr);
    free(ptr); // After this line, you aren't allowed to use the value of ptr any more
    ptr = NULL; // This is not necessary, but it's good practice to ensure that you won't accidentally use the value
}
Run Code Online (Sandbox Code Playgroud)