How can I obtain upper bound of a pointer?

How can I obtain the upper bound of a pointer in the code? For example, I have
a pointer Ptr which points to a block of memory that is allocated by malloc, say,

Ptr = malloc(size);

Then what should I do if I am going to get the upper bound of the memory block
that is pointed to by Ptr in the runtime?


This is rather off-topic for LLVM, it’s a C programming question and the C answer is: you can’t. C doesn’t expose any way to find the upper limit of ‘Ptr’ you just have to keep track of what size you used yourself.

If you want an exact “upper bound”, you’ll have to use something like SoftBound (, which keeps track of the size of each allocated region at runtime.


Both SAFECode () and SoftBound () have run-time support for finding the bounds of an object given a pointer into the object. SAFECode and SoftBound are combined into a single tool available in the SAFECode source code repository (see the SAFECode website for directions on how to download and compile the software). You can play with both of them and see which will suite your needs better. – John T.