0

40 different allocation functions give 40 different call sites

void f00(size_t sz) { void* ptr = malloc(sz); free(ptr); }
void f01(size_t sz) { void* ptr = malloc(sz); free(ptr); }
...
void f39(size_t sz) { void* ptr = malloc(sz); free(ptr); }

An array of those allocation functions. How I need to define?

like this: why (size_t) ?:

  void (*allocators[])(size_t) = { &f00, &f01, ... , &f39 };

or

  void* allocators[] = { &f00, &f01, ... , &f39 };

and what the difference between this two declarations?

Anatoly
  • 1,908
  • 4
  • 25
  • 47

1 Answers1

2

First is correct. In first declaration no need of & in intializers. It should be

 void (*allocators[])(size_t) = { f00, f01, ... , f39 }; 

It declares allocators as an array of pointers to function that return nothing and expects an argument of size_t type.
(size_t) informs the compiler that all functions expects an argument of this type.

Second is wrong because it declares an array of pointers to void.

haccks
  • 104,019
  • 25
  • 176
  • 264