I'm making a dynamic memory allocator and I need to check than when I'm freeing a section of it, that the pointer I'm passing into the function is in fact within that area. I have a pointer to the beginning of the malloc'd area
typedef unsigned char byte;
static byte *memory // pointer to start of allocator memory
which I assign in my initiating function. I also have the size of the malloc'd area stored in
static u_int33_t memory_size; // number of bytes malloc'd in memory[]
How do I ensure that the ptr isnt... (in pseudo code)
ptr < *memory || ptr > *memory + memory_size
and that code results in the following error;
error: comparison of distinct pointer types lacks a cast [-Werror]
if ( object < memory || object > (memory + memory_size))
^
I'm not sure what I need to cast and what not to...
The free function is as follows...
void memfree(void *object)
{
if ( object < memory || object > (memory + memory_size)) {
fprintf(stderr, "vlad_free: Attempt to free via invalid pointer
");
exit(EXIT_FAILURE);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…