Create a custom data type that is a renamed void pointer.
typedef void* myPointerType;
Write a allocation function (which can be a simple wrapper around malloc ) that the user should use to allocate memory.
myPointerType myAllocateFunction (size_t size) { return (myPointerType)(malloc(size)); }
You would also like to create an appropriate βfreeβ function.
Now, your function (the one that runs realloc ) takes the myPointerType object as a parameter instead of a generic pointer. You can drop it to void* for use with realloc . To get around this, the user must manually point the non- malloc ed pointer to myPointerType , but you can indicate in your documentation that listing in and out of myPointerType not allowed (so if they do this and their applications crashes, it is because they misused API).
There are even more powerful ways you can provide this, but I'm not sure if this will be worth the trouble. You cannot completely fool the API (you would be surprised at how fools these days are capable). No matter what you ultimately implement, the best way to make sure your API is being used correctly is to provide clear and complete documentation.
You're in your own way for a pony, sorry.
bta
source share