字符串连接错误与malloc动态内存分配

时间:2015-01-08 18:54:06

标签: c string pic microchip pic32

String * char与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

3 个答案:

答案 0 :(得分:4)

您对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);

答案 1 :(得分: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);

答案 2 :(得分:1)

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

覆盖了它。
    buf = "";

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

while(1) {
    ...    
}

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

free(buf);

所以在随后的无限循环中,你甚至没有buf连接到。