Question

Recently saw someone commending another user on their use of sizeof var instead of sizeof(type). I always thought that was just a style choice. Is there any significant difference? As an example, the lines with f and ff were considered better than the lines with g and gg:

 typedef struct _foo {} foo;

 foo *f = malloc(count * sizeof f);
 foo *g = malloc(sizeof(foo) * count);

 foo **ff = malloc(count * sizeof *ff);
 foo **gg = malloc(sizeof(foo*) * count);

In my opinion, the first set is just a matter of style. But in the second pair of lines, the extra second * can easily be confused for a multiplication.

Was it helpful?

Solution

If the type of the variable is changed, the sizeof will not require changing if the variable is the argument, rather than the type.

Regarding @icepack's comment: the possibility or likelihood of change for type vs. variable name is not the issue. Imagine the variable name is used as the the argument to sizeof and then later changed. In the best case a refactor-rename operation changes all occurrences and no error is introduced. In the worst case an instance of a sizeof is missed and the compiler complains and you fix it. If the type is changed you are done, no possibility of errors at sizeof statements.

Now imagine the type is the argument to sizeof. If the type of the variable is changed, there is no means other than inspection to find all sizeof relating to that variable. You can search, but you will get hits for all the unrelated uses of sizeof of the same type. If one is missed, you will have a runtime problem due to a size mismatch, which is much more trouble to find.

OTHER TIPS

In addition to what Steve says, let me add that sizeof does not evaluate its operand. So you are free to do anything in it. Not only you can use not-yet initialized variables, but you can dereference a null-pointer, call functions not defined but only declared and do any other kind of stuff. I encourage you to always use the expression version for reasons Steve explained greatly.

Also consider that sometimes typenames are really long and unreadable, just think of pointers to functions (especially in C++). Instead of writing sizeof(my_long_type<weird, stuff>) you just do sizeof t.

You may see little difference between these:

foo **ff = malloc(count * sizeof *ff);
foo **gg = malloc(sizeof(foo*) * count);

..but what if the allocation is not nearby to the declaration? If I come across a line like:

ff = realloc(ff, count * sizeof *ff);

then I am reasonably confident that the line is correct, without even remembering the type of ff. However, if I see this:

ff = realloc(ff, count * sizeof(foo *));

then I might be somewhat suspicious, and need to look up the type of ff to put my mind at rest.

The lines f and ff are definitely worse than g and gg. sizeof f is the pointer size, since f is a point. To make them equivalent, you'd have to write sizeof *f (or, for better legibility, sizeof(*f)).

I tend to use sizeof(type) in memory allocation, but invariably sizeof(variable) when using a local or global variable. Nevertheless, there is wisdom in the advice to use the variable name all the time.

I also had a long discussion (rather animated at times, and it lasted over a period of days -- we ended agreeing to differ on the issue) about whether it is OK to use sizeof(variable) or whether it must be sizeof variable; that is, does it matter whether parentheses enclose the argument to sizeof. I've never encountered a compiler that witters about sizeof(variable), so I opt for uniformity and always use the parentheses with sizeof. My colleague was equally adamant that parentheses must not be used with variables; that somehow there is/was a valuable distinction between the notation with and without the parentheses. I've not been persuaded - I don't expect to be persuaded.

On a wholly different tack, a couple of other points about sizeof:

  • If you're using C99 and VLA - variable-length arrays - then sizeof(vla) is not a compile-time constant. Beware!
  • The C pre-processor does not understand sizeof which is a nuisance but logical.

Taking the sizeof a variable can have unexpected results. When the variable is an array, sizeof(array) will return the size of the array, not the size of an individual element or the size of a pointer. Taking the size of a pointer will return the size of a pointer. But since arrays are usually represented as pointers when being passed around, things can get confusing.

Business logic defines your choice.

  1. If your code refers to a particular variable and without this variable your code makes no sense - choose sizeof(var)

  2. If you code deals with a set of variables with particular type - choose sizeof(type). Usually you need this if you have a typedef which defines many variables which you process differently based on their type (e.g. serialization). You may not know which of these variables will remain in future code versions so choosing type as an argument is logically correct. Even changing of such typedef will not affect your sizeof line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top