开发者

How to find out whether the address supplied to free() is an invalid address?

Is there any way to find out whether an address supplied to 开发者_C百科free( ) is an invalid address, before calling the free( )?

We know free( ) creates an undefined behaviour if the address is an invalid address (already freed address). So how to make sure that address is a valid one?

Thanks in advance.


If you need to do this, you have a bigger problem -- you have free'd your memory before you've finished with it, or you've not invalidated references to memory when you've finished with it. Rather than trying to stop your program double-freeing by changing free(), you should be trying to fix your pointer lifetime issues.

You may find that Valgrind can help you to work out where things are going wrong.


The behaviour on free() for invalid address is undefined, because it is unpredictable. In a low-level language like C you cannot say if address is 'valid' or 'invalid'. And there are many ways a address may be invalid. 'Already freed' is only one of the cases other include: address not allocated by malloc, the data is not actually an address (e.g. some integer or character string casted to pointer), data corruption, etc.

If you are interested in the 'already freed' case only you could try to track every memory allocation and deallocation (e.g. by using a custom malloc() and free() wrapper) but this would probably only make your code more complicated and even more error-prone.

Simple answer: no, there is no way to check if the address is invalid. Live with it or switch to a higher level language (like e.g. Python), when you don't care about memory addresses at all.


No.

In order to be more helpful we really need to know your intended usage case.

If it's that you're using the same pointers to sometimes reference memory obtained by malloc, and at other times reference string literals or automatic variables, then you should simply store a flag alongside the pointer that indicates whether it points to something obtained by malloc. Or you could simply store a second copy of the pointer called ptr_to_free that's NULL when the data does not need freeing. This also has the advantage that your "main pointer" could "move around" and point to different parts of the object, since you'd be keeping the original/base pointer to free separately.

If it's just to avoid double-free issues, you just need to fix your code so they don't happen. Setting a pointer to NULL just after you free what it points to is a sloppy trick that usually gets you most of the way there, but you probably have bigger design issues to rethink...


As others have suggested, tracking a pointer is good way of knowing if memory has been freed or not.

I would also suggest the following pointers.

  1. Initialiase the pointer to NULL while you declare it
  2. Once freed you initialise it to NULL again
  3. Check if pointer is not NULL before trying to free.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜