Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a pointer points to allocated memory on the heap

Tags:

People also ask

Are pointers allocated on the heap?

Pointer is allocated on the stack and the object it is pointing to is allocated on the heap.

Is memory allocated for referenced pointers?

Memory is allocated when a pointer is defined. A reference however, is a name alias & hence no memory is allocated for it( Is it correct? ). 2. Reference is bound to be initialized at the time of definition because, a reference is implemented with a constant pointer & hence cannot be made to point to the other object.

Can a pointer point to something on the stack?

(And, yes, if you pull out a plate from somewhere with the stack, you will probably break something.) There are two fundamental operations on a stack: push data-item. This causes the data-item to be placed on the top of the stack and moves the stack pointer to point to this latest item.


I want to know if a pointer points to a piece of memory allocated with malloc/new. I realize that the answer for an arbitrary address is "No you can't" but I do think it is possible to override malloc/free and keep track of allocated memory ranges.

Do you know a memory management library providing this specific tool?
Do you know something for production code?

Valgrind is great, but it is too much instrumentation (slow) and as Will said we don't want to use Valgrind like this (making the soft crash is good enough).
Mudflap is a very good solution, but dedicated to GCC, and sadly, a check does not simply return a boolean (see my answer below).
Note that checking that memory writes are legal is a security issue. So looking for performance is motivated.