Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在这样的函数调用中分配的内存会发生什么变化?_C_Memory_Memory Leaks - Fatal编程技术网

C 在这样的函数调用中分配的内存会发生什么变化?

C 在这样的函数调用中分配的内存会发生什么变化?,c,memory,memory-leaks,C,Memory,Memory Leaks,我有一个关于以下函数和内存分配的问题 char *strjoin(char const *s1, char const *s2) { char *s3; s3 = NULL; if (s1 && s2) { s3 = (char *)malloc(sizeof(char) * (strlen(s1) + strlen(s2) + 1)); if (!s3) return (NUL

我有一个关于以下函数和内存分配的问题

char    *strjoin(char const *s1, char const *s2)
{
    char    *s3;

    s3 = NULL;
    if (s1 && s2)
    {
        s3 = (char *)malloc(sizeof(char) * (strlen(s1) + strlen(s2) + 1));
        if (!s3)
            return (NULL);
        strcpy(s3, s1);
        strcat(s3, s2);
    }
    return (s3);
}

int main()
{
    char    *s1 = "my favorite animal is";
    char    *s2 = " ";
    char    *s3 = "the nyancat";
    char    *res = strjoin(strjoin(s1, s2), s3);
}
strjoins文件如下:

分配(使用malloc(3))并返回一个以 “\0”,s1和s2串联的结果。如果分配 函数返回NULL时失败

在main函数中,函数在此行中调用自身:

char*res=strjoin(strjoin(s1,s2),s3)


由于内存在strjoin(s1、s2)中被分配,但从未分配给任何东西,而且它在外部函数调用中使用,但从技术上讲从未释放,那么内存是否泄漏并未使用?

是的,内部调用确实泄漏内存。通过将返回值存储到位,可以轻松避免泄漏:

char    *intermediate;
char    *res = strjoin(intermediate = strjoin(s1, s2), s3);
free(intermediate);

但是如果这是一个托管系统,当调用
exit
时,操作系统
main
函数/将释放内存。除非您正在编写嵌入式设备、设备驱动程序或操作系统,否则很可能您的目标是托管平台


现在,取决于编译器是否聪明,它实际上可能不会为该程序分配任何内存,因为不需要结果

考虑一下稍有变化的程序:

#include <stdlib.h>
#include <string.h>

static inline __attribute__((always_inline)) char *strjoin(char const *s1, char const *s2)
{
    char    *s3; 
    s3 = NULL;

    if (s1 && s2) {
        s3 = malloc(strlen(s1) + strlen(s2) + 1);
        strcpy(s3, s1);
        strcat(s3, s2);
    }

    return s3;
}

int main()
{
    char    *s1 = "my favorite animal is";
    char    *s2 = " ";
    char    *s3 = "the nyancat";
    char    *res = strjoin(strjoin(s1, s2), s3);
}

(参见中间窗格中的相同程序集),它将具有但不调用“代码> MalOC 甚至一次。

您将其分配给<代码> RES,并且是的,它会泄漏内存,但不,在托管系统中,当<代码>主< /代码>退出时,内存被释放。如果使用优化编译器,它会注意到所有这些都是无稽之谈,并编译一个更简单的版本:
intmain(void){return 0;}
。啊,是的,内部函数调用确实会泄漏内存:\u避免这种事情的简单实用技巧?@P\u J\uuuuu。你在胡说八道。没有副作用。然而,你是对的,GCC/Clang不够聪明,无法深入挖掘。请参阅我的答案以了解更简单的情况。
#include <stdlib.h>
#include <string.h>

static inline __attribute__((always_inline)) char *strjoin(char const *s1, char const *s2)
{
    char    *s3; 
    s3 = NULL;

    if (s1 && s2) {
        s3 = malloc(strlen(s1) + strlen(s2) + 1);
        strcpy(s3, s1);
        strcat(s3, s2);
    }

    return s3;
}

int main()
{
    char    *s1 = "my favorite animal is";
    char    *s2 = " ";
    char    *s3 = "the nyancat";
    char    *res = strjoin(strjoin(s1, s2), s3);
}
int main(void) {
}