Freeing malloced structure in a function

后端 未结 4 1439
一生所求
一生所求 2020-12-21 03:42

I\'m creating a source files containing buffer functionality that I want to use for my other library that I\'m creating.

It is working correctly but I\'m having tro

4条回答
  •  甜味超标
    2020-12-21 04:35

    If any attempt is made to read memory that has been freed can crash your program. Or they might not. As far as the language is concerned, its undefined behaviour.

    Your compiler won't warn you about it(or stop you from accessing it). But clearly don't do this after calling free -

    printf("buff total size: %d\n", buff->total_size);
    

    As a good practice you can set the freed pointer to NULL .

提交回复
热议问题