C - allocating an array of pointers, and usage - typecast safety -


i'm working legacy code makes extensive use of kind of thing:

// allocate look-up-table of pointers. long *pointerlut = (long *) malloc(sizeof(long) * numpointers);  ...   // populate array pointers. (int i=0; i<numpointers; i++) {      pointerlut[i] = (long) newfoo(); }  ...   // access lut. foo *foo = (foo *) pointerlut[anindex]; 

as can see, allocates array of longs, idea of using them generic pointer storage.

q1. approach safe?

q2. style-wise, how improved? need be? (the typecasting rattles fear-monkey in me.)

thanks.

edit: missed said "generic pointer storage" in question. answer not correct case.

if working pointers foo that's code should say.

// allocate look-up-table of pointers. foo **pointerlut = (foo **) malloc(sizeof(foo *) * numpointers);  // populate array pointers. (int i=0; i<numpointers; i++) {      pointerlut[i] = newfoo(); // newfoo() should return (foo *) }  // access lut. foo *foo = pointerlut[anindex]; 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -