I'm creating a general linked list and I want it to be as portable as I can. The idea is to have a struct BUCKET with the next and previous pointers in it and allocate the members contained in the list just "above" the BUCKET structs.
So I have:
Code:
typedef struct BUCKET
{
struct BUCKET *Next;
struct BUCKET **Prev;
} BUCKET;
and a list struct
Code:
typedef struct DATA_LIST
{
BUCKET *First;
} DATA_LIST;
and when you want to add an item to the list you have to use a function newnode.
Code:
void *newnode(size_t Size)
{
BUCKET *node = calloc(Size + sizeof *node, 1);
return node ? node + 1 : NULL;
}
So when you want a linked list of X structures you do:
Code:
struct X { int X; long double Y; };
struct X *newnode = newnode(sizeof *newnode);
addtolist(list, newnode);
where addtolist only knows about the BUCKET members.
But the problem I have is with alignment. newnode returns a pointer just above the alignment of the BUCKET struct (BUCKET *+ 1) but what if the first member of X lands on a non aligned address?
How to solve this with standard C?
I hope I'm clear