Freeing malloc will not erase char data

前端 未结 4 1848
悲&欢浪女
悲&欢浪女 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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!

提交回复
热议问题