GeekInterview.com
Series: Subject: Topic:
Question: 524 of 587

Why should we assign NULL to the elements (pointer) after freeing them? 

This is paranoia based on long experience. After a pointer has been freed, you can no longer use the pointed-to data. The pointer is said to dangle; it doesnt point at anything useful. If you NULL out or zero out a pointer immediately after freeing it, your program can no longer get in trouble by using that pointer. True, you might go indirect on the null pointer instead, but thats something your debugger might be able to help you with immediately. Also, there still might be copies of the pointer that refer to the memory that has been deallocated; thats the nature of C. Zeroing out pointers after freeing them wont solve all problems;  
Asked by: Interview Candidate | Asked on: Mar 6th, 2005

This Question is not yet answered!

Related Open Questions

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.