I did try the free(q), but was surprised when the following thing happened:
when there were still items on my queue, i did a free(q) and then tried to access items on my queue, it turns out that the first remaining item was not accessible but the 2nd, 3rd item so on were accessible. any explanation for this?
Once you'vefreed it, consider all of it inaccessible. If it appears that items are still accessible, that's just a temporary illusion to make you think you're doing something you're allowed to.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>so if I had done some other things later on in my program
Once freed, the memory is no longer yours. That means that in theory, another process could allocate the memory immediately after you're done freeing it and overwrite the values you had stored there before printf is called in your program. So your print isn't guaranteed to work, and on top of that, you've invoked undefined behavior because you don't own the memory you're accessing.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401