3

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.

3
  • use size_t instead of long...
    – smerlin
    Dec 2, 2010 at 23:26
  • 1
    @smerlin: uintptr_t would be a better option. If a C implementation supports the ability to convert a pointer type to an integer and back without loss of information it will probably provide uintptr_t. The difference between uintptr_t and size_t is that if uintptr_t is provided, it is guaranteed to hold any valid pointer to void, and guaranteed that when converted back to a pointer to void it shall compare equal to the original.
    – dreamlax
    Dec 2, 2010 at 23:29
  • @dreamlax: yes, but it isnt provided on every compiler, so using your own typedef, which uses uintptr_t or intptr_t on the compilers where those types are available and size_t on others will be the best solution... however most of the times size_t and uintptr_t will represent the identical type.
    – smerlin
    Dec 2, 2010 at 23:38

4 Answers 4

5

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.

// Allocate a look-up-table of pointers.
Foo **pointerLUT = (Foo **) malloc(sizeof(Foo *) * numPointers);

// Populate the array with pointers.
for (int i=0; i<numPointers; i++) {
     pointerLUT[i] = NewFoo(); // NewFoo() should return (Foo *)
}

// Access the LUT.
Foo *foo = pointerLUT[anIndex];
4
  • The question says that the table is used for generic pointer storage. I guess perhaps it is not known whether each pointer in the look-up table will point to a Foo.
    – dreamlax
    Dec 2, 2010 at 23:42
  • @dreamlax: You are correct, I didn't see that. Looks like he is stuck with void ** Dec 2, 2010 at 23:45
  • Thanks to all for your help. Blastfurnace is correct in saying that the best thing to do would be to use specific types. What I meant by "generic" is that the programmer has used this approach across the project, as a one-size-fits-all solution for pointer arrays, typecasting to the required type for that usage. However, for each individual use, only one type of object is being referred to. e.g. Foo. I shall bite the bullet and rewrite using Blastfurnace's approach. Cheers.
    – SirRatty
    Dec 3, 2010 at 0:03
  • How do you check for successful (malloc()) allocation? poinerLUT != NULL, yes?
    – Ziezi
    Jun 16, 2017 at 9:37
5

A1: You should replace long with void * because sizeof(long) is not necessarily the same as sizeof(void *). For example this would not work on 64-bit Windows setup, where long is 32 bit and pointers are 64.

A2: If you use C and void * you would not need to use typecasts, since it's ok to cast from and to void *.

4

In C, conversions from pointer to integer types (and vice versa) are implementation defined1 (this means that the implementation must document whether this is supported and/or how it is done). Also there is no guarantee that a pointer to void has the same size or can represent all the same values as a long integer.

It would be better to allocate an array of pointer to void type, for example:

void **pointerLUT = malloc(sizeof (void *) * numPointers);

// Populate the array with pointers.
for (int i=0; i<numPointers; i++) {
    pointerLUT[i] = NewFoo(); // implicit conversion to void *
}

// Access the LUT.
Foo *foo = pointerLUT[anIndex]; // implicit conversion from void *

1: See section §6.3.2.3 paragraphs 5 and 6:

(5) An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

(6) Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

3
  • You want void **pointerLUT, not void *pointerLUT. As written, pointerLUT[anIndex] is an error because you can't dereference a void*. Dec 2, 2010 at 23:40
  • If pointerLUT is a void pointer I don't think you can subscript it that way. Dec 2, 2010 at 23:41
  • @Adam Rosenfield: Whoops! Good catch.
    – dreamlax
    Dec 2, 2010 at 23:43
1

That code is unportable because there is no guarantee that sizeof(long) == sizeof(void*). The code would be better in style if it at least used void* instead of long, but that won't be an easy fix to make everywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.