简体   繁体   English

释放由 function 分配的 char*

[英]Free a char* that was malloc'd by a function

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

char* mkstr(char str1[], char str2[])
{
        char* out = malloc(sizeof(*str1) + sizeof(*str2) + 1);
        strcpy(out, str1);
        strcat(out, str2);

        return out;
}

int main()
{
        char* str = mkstr("i use ","arch btw");
        printf("%s\n",str);
}

When main() calls mkstr() , mkstr() will malloc a char* called out .main()调用mkstr()时, mkstr()out调用char* How can I free(out) properly from this code?我怎样才能从这段代码中正确地free(out) Can I just leave it be, or will the OS just free up the malloc'd space?我可以保留它,还是操作系统会释放 malloc 的空间?

Is this the best way to do it, or are there better ways of doing it?这是最好的方法,还是有更好的方法?

I'm on Linux (if that's relevant).我在 Linux 上(如果相关的话)。

sizeof(*x) is the size of a pointer on your platform. sizeof(*x)是平台上指针的大小。 It's usually 4 on a 32 bit platform and 8 on a 64 bit platform.在 32 位平台上通常为 4,在 64 位平台上通常为 8。

To get the length of a string you need to use the strlen function.要获取字符串的长度,您需要使用strlen function。

Corrected code:更正的代码:

char* mkstr(char str1[], char str2[])
{
        // you need to use strlen to get the length of a string
        char* out = malloc(strlen(str1) + strlen(str2) + 1);

        strcpy(out, str1);
        strcat(out, str2);
        return out;
}

int main()
{
        char* str = mkstr("i use ","arch btw");
        printf("%s\n",str);
        free(str);           // simply free str
}

Theory:理论:

Every heap allocated object should be freed, before exiting the application (most of the modern operating system manages the heap allocation even if you didn't freed them at exiting the application).在退出应用程序之前,应该释放每个分配 object 的堆(大多数现代操作系统都会管理堆分配,即使您在退出应用程序时没有释放它们)。 By the way freeing heap resources is a good practice though.顺便说一句,释放堆资源是一个很好的做法。

Problems in your code:您的代码中的问题:

  1. Parameters for mkstr function should be (const char *str1, const char *str2) instead of (char str[], char str2[]) . mkstr function 的参数应该是(const char *str1, const char *str2)而不是(char str[], char str2[])
  2. Use calloc instead of malloc for better safety.使用calloc代替malloc以获得更好的安全性。
  3. Use strlen function to determine the length of the string, instead of sizeof .使用strlen function 来确定字符串的长度,而不是sizeof
  4. Set void or (int argc, char const **argv) as the parameter of main function.void(int argc, char const **argv)设置为main function 的参数。

Now `free` heap allocations:现在`free`堆分配:

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

char *mkstr(const char *str1, const char *str2)
{
    char *out = calloc(sizeof(char) * (strlen(str1) + strlen(str2) + 1), sizeof(char));
    strcpy(out, str1);
    strcat(out, str2);
    return out;
}

int main(int argc, char const **argv)
{
    char *str = mkstr("i use ", "arch btw");
    printf("%s\n", str);
    free(str); // freed the heap allocated resource before exiting
    return 0;
}

Anyways, after reading all your answers, this is the new code.无论如何,在阅读了所有答案之后,这是新代码。

char* mkstr(char str1[], char str2[])
{
        char* out = malloc(strlen(str1) + strlen(str2) + 1);
        strcpy(out, str1);
        strcat(out, str2);

        return out;
}

int main()
{
        char* str = mkstr("i use ","arch btw");
        printf("%s\n",str);
        free(str);

        return 0;
}

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

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