I have a function that recieves a pointer to dynamic array of 100 ints. But instead of 100 I have just 50 allocated by malloc or calloc before that.
Is there a way that I could check if any ellement (like 79th for example) is allocated rather than wonder what this SIGSEGV actually means ?
My question is purely theoretic and I have no actual code to show.
Well, you could do such a thing according to your description, given an array and looking for an index (which is slightly different from “any raw pointer”). And with some more work, it is even possible to do such a thing for any pointer.
The
mallocfunction necessarily stores information about how much was allocated. Unluckily, there is no standard how this must be done. Some compilers over-allocate and store the size immediately preceding the allocated data. Others may store addresses in a map, yet others may do something else, you don’t know.However, most (all?) C libraries and at least one linker that I know of have explicit support for overloading/hooking/replacing allocation functions.
For example in the GNU C library, you can set
__malloc_hook. and GNUldlets you do such a thing at linker level with__wrap_malloc.You could thus overload/hook
mallocandfreewith a function that simply calls the realmallocfunction and stores the information how much was allocated yourself somewhere (e.g. by over-allocating and using the first word, or whatever you like).Then write a function which takes a base pointer and an index. That function looks at the allocation info (now you know where to find it!), and can trivially check whether the index is in range. This does not work for “just any pointer”.
An alternative solution which works for “just any pointer” would be to write an allocator that satisfies allocations from separate arenas rather than simply wrapping the real
malloc. All allocations coming from the same arena have the same allocation size. Given any pointer, you would then only need to iterate over all your arenas and look whether the address is within the arena’s start and end address.However, one should normally be quite sure how much one has allocated, this should not be guesswork, or random luck, or something to figure out at runtime.
Also, given the presence of ready-to-use memory debuggers, I doubt it is really worth investing time in doing such a thing application-side. Just use something like valgrind, no need to write any code at all.