【问题标题】:String concatenate error with malloc dynamic memory allocation字符串连接错误与 malloc 动态内存分配
【发布时间】:2015-01-08 18:54:05
【问题描述】:

String *char concatenate error with malloc 动态内存分配

我想做一个函数来连接字符串,它可以工作但它给出了一个错误并且处理器重新启动,我认为指针有问题,但我不知道它是什么,内存分配问题。

提前致谢!

char *buf;

int main(void) {
    // ...

    WriteString("#INIT.\r\n"); //serial output

    buf = "";

    while(1)
    {
        char *str1 = "qwe";
        char *str2 = "asd";
        char *str3 = "zxc";
        char *str4 = "123";

        buf = my_strcat(buf,str1);
        buf = my_strcat(buf,str2);
        buf = my_strcat(buf,str3);
        buf = my_strcat(buf,str4);

        WriteString(buf); //serial output

        free(buf);
    }
}

char *my_strcat(const char *str1, const char *str2) {
    char *new_str;
    new_str = malloc(strlen(str1)+strlen(str2)+1);
    new_str[0] = '\0';
    strcat(new_str,str1);
    strcat(new_str,str2);
    return new_str;
}

串行输出...

#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
#INIT.
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123
qweasdzxc123

【问题讨论】:

    标签: c string pic microchip pic32


    【解决方案1】:

    您对my_strcat 的第一次调用具有未定义的行为,因为之前未初始化buf。正是这一行是问题

    new_str = malloc(strlen(str1)+strlen(str2)+1);
    

    strlen(str1) 其中str1 未初始化。

    建议,使用realloc

    char *my_strcat(char *str1, const char *str2)
    {
        char  *new_str;
        size_t length;
        size_t str1length;
    
        if (str2 == NULL)
            return str1;
        str1length = 0;
        if (str1 != NULL)
            str1length = strlen(str1);
        length  = strlen(str2) + str1length;
        new_str = realloc(str1, 1 + length);
        if (new_str == NULL)
            return str1;
        new_str[str1length] = '\0';
    
        strcat(new_str, str2);
    
        return new_str;
    }
    

    char *buf;
    char *str1 = "qwe";
    char *str2 = "asd";
    char *str3 = "zxc";
    char *str4 = "123";
    
    buf = NULL;
    buf = my_strcat(buf, str1);
    buf = my_strcat(buf, str2);
    buf = my_strcat(buf, str3);
    buf = my_strcat(buf, str4);
    

    【讨论】:

    • 将这一行 buf = ""; 移动到循环中,因为在随后的调用中,您使用的是 free'd buf,但您的解决方案仍然会泄漏内存,因为 R Sahu 的回答指出,你应该试试我的realloc版本。
    • 工作!你太棒了!谢谢!
    • iharob, *buf 必须是全局的,才能被其他方法访问,例如打印缓冲区。
    • 这不是一个好的理由,让每个需要访问buf 的函数都带一个char 指针参数并将buf 传递给它,一个适用于全局变量的函数不会没有多大意义,例如,您不能重用它的代码。尽量避免使用全局变量。
    【解决方案2】:

    您在 while 循环中有内存泄漏并且内存不足。

        buf = my_strcat(buf,str1); // Got some new memory
        buf = my_strcat(buf,str2); // Got some more new memory without freeing the previous memory
                                   // The previous memory is lost. You don't even have a pointer 
                                   // to it any more.
    
        buf = my_strcat(buf,str3); // Ditto
        buf = my_strcat(buf,str4); // Ditto
    

    你需要什么:

        char* temp = NULL
        buf = my_strcat(buf,str1);
        temp = buf;
    
        buf = my_strcat(temp,str2);
        free(temp);
        temp = buf;
    
        buf = my_strcat(temp,str3);
        free(temp);
        temp = buf;
    
        buf = my_strcat(temp,str4);
        free(temp);
    

    【讨论】:

    • 确实存在内存泄漏,但是否会在大约 15 次迭代后导致该问题?
    • @chux,关于 15 次迭代的好点。 OP 发布的代码中可能存在其他问题。他们的输出与代码不匹配。
    【解决方案3】:

    您对buf 的使用不合逻辑。你还没有展示它是如何分配的,但即使你为buf 做了malloc() 内存,你也用

        buf = "";
    

    然后,你有一个没有退出条件的无限循环

    while(1) {
        ...    
    }
    

    它将继续尝试连接,直到计算机着火。更糟糕的是,在while() 循环结束时

    free(buf);
    

    因此,在随后的无限循环中,您甚至没有要连接的 buf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 2011-01-15
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      相关资源
      最近更新 更多