It sounds like you have two problems.
Cannot dereference a void pointer. Somewhere in your code you have used the result from
allocateMemory()
without a cast. The code you give is OK, but whatever line the compiler is flagging as wrong is not OK. For example, maybe you have:void *foo = allocateMemory(); foo[42]; // compiler doesn't have a real type here - error ((int*)foo)[42]; // compiler happy
Attempted to access protected memory. Somewhere in your code you have an invalid pointer. The most likely cause is that
allocateMemory()
is returning NULL (which you are not checking for).
Your general approach seems OK to me; the issues you describe are related to details in your code, not the overall idea.