In your code
struct buf_stats *bs = malloc(sizeof(struct buf_states*)) ;
is wrong for many reasons, like
- You are using an undefined type (as you mentioned)
- You are allocating way less memory (allocating for a pointer-to-type instead of the type)
But you compiler can't help much in _this_case for this particular type of error, as
a pointer to (any) type in a platform has a defined size, for that the structure (i.e. the type of the variable to which it points to) need not be complete (defined). This is the reason we can have self-referencing structures, right?
malloc()
has no idea about the target variable type. It just reads the argument for the needed size, return a pointer (which is of type void *
) to the allocated memory and upon assignment, that gets changed to the target type. It cannot possibly calculate the mismatch in the target size (type) with the allocated memory size.
Most convenient and simplest way to avoid these type of mistakes is, not to use the hard-coded type directly as the operand of sizeof
, rather, use the variable reference.
Something like
struct buf_stats *bs = malloc(sizeof *bs) ; // you can write that as (sizeof (*bs)) also
// sizeof *bs === sizeof (struct buf_stats)
which is equivalent to
struct buf_stats *bs = malloc(sizeof(struct buf_stats)) ;
but is more robust and less error prone.
Notes:
- You don't need the parenthesis if the operand is not a type name.
- This statement does not need any modification upon changing the type of target variable
bs
.