1

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.

So, for example, let's say the user requests 1024B with the smallest block size of 8B. That means the possible block sizes would be 1024, 512, 256, 128, 64, 32, 16, and 8.

To keep track of the free blocks of memory, I have an array of pointers to structures. These structures are called Header, and the array is called FreeList. What I mean is that FreeList[0] would contain a pointer to the space in memory where there is a block of memory size 8. FreeList[1] would contain a pointer to the space in memory where there is a block of memory size 16. etc.

typedef void * Addr;
struct Header
{
    Addr next;
    int order;
};

struct Header *FreeList[];

I'm trying to allocate memory for this free list to use with the following code:

FreeList = malloc(Order*sizeof(struct Header));

Where Order is the number of different block sizes you can have.

I'm getting the compile error 'FreeList' has an incomplete type.

I don't want these pointers to point anywhere yet, I just want to allocate the space for the data.

3
  • 2
    Is this the real code ? You are missing a ; at the end of struct definition. Also if it is an array of pointers, what is the size of the array ? Why haven't you specified anything in the indexes ? Commented Jul 10, 2012 at 21:03
  • Is the definition of Header included in the translation unit (.c) that does the allocation? If not, Header can only be used as a pointer - the compiler does not know its size. Commented Jul 10, 2012 at 21:06
  • Yes this is all within the same .c file. Commented Jul 10, 2012 at 21:08

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this works. I just needed an array of pointers to headers. The headers themselves are within the user memory.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.