tags:

views:

2192

answers:

6

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.

+15  A: 

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

Steve Fallows
For array variables, sizeof(array) will give you the real size of the array. It can be used to calculate the number of elements sizeof(array)/sizeof(element). Note that it won't work for dinamically allocated memory through a pointer.
David Rodríguez - dribeas
+2  A: 

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)).

Martin v. Löwis
+2  A: 

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.

Johannes Schaub - litb
A: 

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.

SoapBox
While the first line is misleading: there is nothing unexpected in the result, the second sentence is missing from all other answers: sizeof(array) will give you the size of the whole array and not an element. It can be used to compute the number of elements sizeof(array)/sizeof(element)
David Rodríguez - dribeas
@dribeas: yes, hence'#define DIM(x) (sizeof(x)/sizeof(*(x)))' for a variety of possible macro names (ARRAY_SIZE, DIMENSION, ...).
Jonathan Leffler
A: 

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.
Jonathan Leffler
Looks like you've got another askable question here. My understanding is sizeof is a compile-time thing, even though it kinda looks and acts like a (library) function. So maybe he wants it to look different. I don't use it enuf to matter. Then again I've gotten stares from doing "return(x);"
gbarry
I most often use parentheses around the return value. I know some people are aghast at that, but it works for me. I'm not confused about either return or sizeof being 'functions'.
Jonathan Leffler
I hate parentheses on return, mostly because it is unnecessary. Having parentheses after sizeof can help readability: malloc(sizeof(*f)*10) is much easier to understand than malloc(sizeof *f*10).
jmucchiello
A: 

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.

caf