Freeing malloc will not erase char data

前端 未结 4 1916
悲&欢浪女
悲&欢浪女 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: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.

提交回复
热议问题