A very often executed piece of code has the following calculation :
long *lp
char *ep, *cp
...
tlen = (ep - cp) / sizeof (*lp);
Would changing this to:
long *lp
char *ep, *cp
...
tlen = (ep - cp) / sizeof (long);
result in any more efficiency (since the sizeof of calculated at compile time) or would a modern compiler handle this at compile time already. what does gcc do ?
The
sizeofoperator is always a compile time evaluated construct 0, so there is no difference.The fragment …
will therefore be transformed into something not unlike …
(assuming that
sizeof(long)==41.), with optimizations applied the next transformation is probably …More optimizations to come, of course; it’s just a demonstration of a possible consequence of it being a compile time construct 0.
I would always prefer
"sizeof(_var-name_)"oversizeof(_typename_), as its more generic and doesn’t require manual adjustment when you change the type of the variable (except when you change from array to pointer).0: Except for variable length arrays.
1: Size differs with platform