I’m working with some legacy code that makes extensive use of this kind of thing:
// Allocate a look-up-table of pointers.
long *pointerLUT = (long *) malloc(sizeof(long) * numPointers);
...
// Populate the array with pointers.
for (int i=0; i<numPointers; i++) {
pointerLUT[i] = (long) NewFoo();
}
...
// Access the LUT.
Foo *foo = (Foo *) pointerLUT[anIndex];
As you can see, this allocates an array of longs, with the idea of using them generic pointer storage.
Q1. Is this approach safe?
Q2. Style-wise, how could it be improved? Does it need to be? (The typecasting rattles the fear-monkey in me.)
Thanks.
EDIT: I missed where he said “generic pointer storage” in the question. This answer is not correct for that case.
If you are working with pointers to Foo then that’s what your code should say.