I put the whole code but, of course, it's in different files (.h and .c files)
typedef unsigned char ubyte;
typedef unsigned int uint;
#include<stdbool.h>
typedef struct Cube {
ubyte n;
ubyte e;
ubyte s;
ubyte w;
} Cube;
typedef struct Piece {
Cube c;
bool is_main;
char offset_n;
char offset_s;
} Piece;
typedef struct Block {
ubyte total;
Piece* pieces;
} Block;
Block *block_create(uint nb_pieces) {
Block *block = malloc(sizeof(Block) + (sizeof(Piece) * nb_pieces));
block->pieces = (Piece *) (&block + sizeof(Block));
return block;
}
I am just wondering if this line of code: block->pieces = (&block + sizeof(Block)); will be always safe. I mean: can we be sure that, immediately after the sizeof(Piece), we will have precisely (sizeof(Piece) * nb_pieces)? Are we sure there will never be an alignment problem (ie maybe if it's 64-bits aligned, the memory for sizeof(Block) will be less than 8 bytes and the block->pieces should not point to exactly sizeof(Block), but "sizeof(Block) 64 bits-aligned").
I hope I'm clear enough.