Nooby question about free

Hi ,

can free function be used without malloc?

That is ,
1
2
3
Student *ptr = stdList->head ;
...
free(ptr);



the code above is correct?
You cannot free memory that has been dynamically allocated with malloc. That staed, if you have allocated previously in your program stdList->head with malloc(), freeing ptr should work fine (just remember not to reference stdList->head anymore)
@NT3
You cannot free memory that has been dynamically allocated with malloc.


Did you mean
You cannot free memory that has not been dynamically allocated with malloc?


:)

Also maybe set ptr = nullptr after free, but as pointed out above, Murphy's law guarantees that someone's going to try and use stdList->head and cause an access violation.
closed account (N36fSL3A)
I'm assuming that he's using C since he referenced C functions, and nullptr doesn't exist in C from my knowledge.
Topic archived. No new replies allowed.