Freeing malloc will not erase char data

前端 未结 4 1849
悲&欢浪女
悲&欢浪女 2021-01-29 07:27

I made a smaller scenario of my bigger problem. What I try to do is pass a string to a function which will make a new string out of it. However I ran into some problems.

相关标签:
4条回答
  • 2021-01-29 08:07

    Try changing

    strcat(buff, "."); 
    

    into

    strcpy(buff, "."); 
    

    Or alternativly initialise buff when declaring it like so:

     char buff[1024] = "";
    
    0 讨论(0)
  • 2021-01-29 08:23
    string* newStr=malloc(sizeof(string)); // malloc a newStr
    strcpy(*newStr, msg);            // copying msg to newStr
    

    This will also crash. string is a pointer so sizeof of it will return 4 or 8, not what you wanted to do.

    Ok, forget about my remark you made a typedef, but I let it here to show you why the typedef is a bad idea. At first glance it obfuscated the fact that it was an array and not a pointer, on a 30 line program it's not a probleme, but when you have to maintain a 200 000 lines project (as I do), you will start to hate these kind of things.

    Another point, you should avoid to work with fixed sized strings of 1024 bytes. 1024 is not that big (even homecomputer of the 80s had screens bigger than that) and for the majority if strings, which are fairly short, you spoil a lot of memory for nothing.

    0 讨论(0)
  • 2021-01-29 08:31

    You are using buff without initializing it. Try:

    char buff[1024];
    buff[0] = 0;
    

    Something else I find weird is that I can printf the value of newStr just after I have freed it.

    Accessing memory after freeing it is undefined behavior. Typically, for performance reasons, free doesn't zero the memory.

    That's 2 cases of undefined behavior in the same question + one really weird typedef. Keep it up!

    0 讨论(0)
  • 2021-01-29 08:33

    You should clear content of buf[1024] each iteration.


    UPDATE

    Because buf[1024] will not be zero-ed automatically when allocated on the stack. And you choose strcat to concatenate two string, which will find a \0-terminate. Thus if buf contains some default value, it will introduce obfuscated output.

    Use buf[1024] = ""; to allocate a buffer will correct the output.

    0 讨论(0)
提交回复
热议问题