I\'m trying to build a memory allocator in C. The user starts by saying how much memory he wants to use, and the smallest block size of memory that can be made available.
<In C language
struct Header *FreeList[];
is a tentative definition for a static array of unknown size (incomplete type). This array should be defined later with known compile-time size. The point is that it is a static array. It is not "allocatable" by malloc
.
If you need an array of pointers that can be allocated at run-time by malloc
, you have to declare a pointer-to-pointer variable
struct Header **FreeList;
which is latter allocated with proper size
FreeList = malloc(Order * sizeof *FreeList);
Note that in this case you are allocating an array of pointers, just like you wanted. And the sizeof
in the above allocation is equivalent to sizeof(struct Header *)
. i.e. size of a pointer (as opposed to the incorrect sizeof(struct Header)
in your original code).
This, again, allocates an array of uninitialized pointers. It is your responsibility to initialize these pointers, i.e. to make them point wherever you want them to point to. If necessary, you'll have to allocate memory for the actual headers as well.
However, it is not really clear from what you posted whether you really need an array of pointers to headers or, maybe, an array of actual headers. Your explanation is confusing and, at times, self-contradictory. If you need an array of actual headers, then the pointer declaration and allocation will look as follows
struct Header *FreeList;
...
FreeList = malloc(Order * sizeof *FreeList);
In this case sizeof
expression above is equivalent to sizeof(struct Header)
, as in our original example. Remember though that the allocated header array is still not initialized.