Cleaning that up a little bit, we get
ServiceMsg *msg = malloc(sizeof *msg);
which is exactly the same as
ServiceMsg *msg = malloc(N * sizeof *msg);
where N == 1
. This is a fairly common idiom in C for dynamically allocating memory:
T *p = malloc( N * sizeof *p ); // for any non-function type T
or
T *p;
...
p = malloc( N * sizeof *p );
or
T *p = calloc( N, sizeof *p );
or
T *p;
...
p = calloc( N, sizeof *p );
It has the advantage of not repeating type information, so maintenance is easier.
The cast is unnecessary1, and since the type of the expression *msg
is ServiceMessage
, sizeof *msg == sizeof( ServiceMessage )
.
Remember that sizeof
is an operator, not a function - parentheses are only required if the operand is a type name like ServiceMessage
or int
or char
, or if it's an arithmetic expression like 1 + 2
. Since sizeof
is a unary operator it has higher precedence than an arithmetic operator, so sizeof 1 + 2
is parsed as (sizeof 1) + 2
, which probably isn't what you want.
- Unless you're compiling this code as C++ or using an ancient K&R implementation. Prior to C89, the
*alloc
functions returned char *
instead of void *
, so a cast was necessary if the target wasn't a char *
. C++ does not allow implicit conversion from void *
to other pointer types, so it requires a cast as well, but if you're writing C++, you shouldn't be using C-style memory management anyway.