Function free() in C isn't working for me

前端 未结 5 667
无人共我
无人共我 2020-12-18 17:25

I have been trying to free memory allocated via malloc() using free().

Some of the structs it does free but leaves some the way they were a

5条回答
  •  庸人自扰
    2020-12-18 18:16

    Calling free does not set the pointer to NULL. You have to do that yourself.

    7.21:  Why isn't a pointer null after calling free()?
        How unsafe is it to use (assign, compare) a pointer value after
        it's been freed?
    
    A:  When you call free(), the memory pointed to by the passed
        pointer is freed, but the value of the pointer in the caller
        probably remains unchanged, because C's pass-by-value semantics
        mean that called functions never permanently change the values
        of their arguments.  (See also question 4.8.)
    
        A pointer value which has been freed is, strictly speaking,
        invalid, and *any* use of it, even if it is not dereferenced,
        can theoretically lead to trouble, though as a quality of
        implementation issue, most implementations will probably not go
        out of their way to generate exceptions for innocuous uses of
        invalid pointers.
    
        References: ISO Sec. 7.10.3; Rationale Sec. 3.2.2.3.

提交回复
热议问题