c - 指针作为函数返回和 malloc 的使用

标签 c pointers function-pointers free

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

主要功能:

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

添加函数:

int* add(int *a, int *b)
{
    int c;
    c = *(a)+*(b);
    return &c;
}

这工作正常并给我输出 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);
}

输出将不再是 30 并且由于堆栈帧被释放而未定义..所以我必须像下面这样使用 malloc()

修改我的程序
int* add(int *a, int *b)
{    
    int* c = (int*)malloc(sizeof(int));
    *c = *(a)+*(b);
    return c;
}

这有效。

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

free(c);

如果我在 main 中使用 free()c 不在范围内并且它不会工作,如果我在 add 中使用 'free`,我会再次得到未定义的结果。

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

free(C);

复现总计

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

}

最佳答案

int* add(int *a, int *b)
{
    int c;
    c = *(a)+*(b);
    return &c;
}

This works correctly and gives me output 30..

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

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

如果你想在一个函数中分配一些内存并返回一个指向它的指针,那么在函数中调用malloc是正确的。由 malloc 分配的内存一直有效,直到您对其调用 free。由于您需要在使用完内存后调用 free,因此这必须在 add 返回后发生:您确实需要调用 freemain 中。能够从不同的函数调用 mallocfree 是动态内存分配的重点。

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

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
}

关于c - 指针作为函数返回和 malloc 的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46266963/

相关文章:

C 两个函数之间的时间差

c - 库支持非常高动态范围的 TIFF 文件?

c++ - C++中指针数组的语法

c - 为什么这个简单的 Linux C 程序在运行时加载 .so 会崩溃?

c - c中的逆累积分布函数?

c - 为什么在 C 语言中退出 do-while 循环请求输入后程序会崩溃?

function - *(*uintptr) 和 **(**uintptr) 有什么区别

c++ - 将指向结构成员的指针传递给函数

c++ - 使用 C 的回调接口(interface)时避免在 C++ 中使用静态成员函数

c - 你如何编写一个不使用 typedef 的返回函数指针的函数指针?