Some people think it's a good safety measure, other people think it is not. In any case, not everyone zeroes out pointers after freeing them. Making an exception to this language rule just for free would be crazy, even if it were possible. This is part of the design of the C language - when you call a function passing a value, then the callee cannot tell how that value was computed, or what variable might contain it in the caller's code. You're just passing the address currently stored in it. It has no way of modifying ptr, because you aren't passing the variable ptr itself into free. If so, then the reason free can't set ptr to NULL, is that free only receives the value from the variable ptr. So if you're doing that, then you have badly misunderstood memory allocation.īut I'm guessing when you say "we set it to zero after freeing", you're maybe talking about code like this: free(ptr) Attempting to do it yields undefined behaviour. If you mean, as others have assumed, setting 0 for every byte of the memory block being freed, then you cannot do that after freeing the block.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |