sizeof

F

Frederick Gotham

Richard posted:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",


If "x" is a type, then the parentheses are mandatory:

int i = sizeof(int); /* OK */

int i = sizeof int; /* Compile Error */

If "x" is an expression, then "sizeof" works just like any other unary
operator:

&x = address of x

!x = logical inversion of x

sizeof x = size of x

I omit redundant parentheses wherever possible in my code, and so I write:

p = malloc(len * sizeof *p);

Using parentheses with "sizeof" implies that it's a function rather than an
operator -- which is misleading.
 
R

Richard

Frederick Gotham said:
Richard posted:



If "x" is a type, then the parentheses are mandatory:

int i = sizeof(int); /* OK */

int i = sizeof int; /* Compile Error */

If "x" is an expression, then "sizeof" works just like any other unary
operator:

&x = address of x

!x = logical inversion of x

sizeof x = size of x

I omit redundant parentheses wherever possible in my code, and so I
write:

I include them wherever possibe.


So at the end of the day "sizeof x" and "sizeof(x)" are identical?
 
L

Lew Pitcher

Richard said:
Frederick Gotham said:
Richard posted:



If "x" is a type, then the parentheses are mandatory:

int i = sizeof(int); /* OK */

int i = sizeof int; /* Compile Error */
[snip]
So at the end of the day "sizeof x" and "sizeof(x)" are identical?

Nope.

At the end of the day,
sizeof(x)
works under all conditions, and
sizeof x
only works if x is not a type
 
R

Richard Heathfield

Richard said:
Could someone point me to why "sizeof x" is/isnt preferable to
"sizeof(x)",

Would you agree that, for most purposes, j + k is preferable to (j) + (k) ?
 
R

Richard

Richard Heathfield said:
Richard said:


Would you agree that, for most purposes, j + k is preferable to (j) + (k) ?

I have no idea if you are being a smartass there or not. I fail to see
the relation.
 
R

Richard

Lew Pitcher said:
Richard said:
Frederick Gotham said:
Richard posted:

Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",


If "x" is a type, then the parentheses are mandatory:

int i = sizeof(int); /* OK */

int i = sizeof int; /* Compile Error */
[snip]
So at the end of the day "sizeof x" and "sizeof(x)" are identical?

Nope.

At the end of the day,
sizeof(x)
works under all conditions, and
sizeof x
only works if x is not a type

I'll stick with sizeof(x) then. Thanks.

--
 
R

Richard Heathfield

Lew Pitcher said:

At the end of the day,
sizeof(x)
works under all conditions, and
sizeof x
only works if x is not a type

Very true, but not all that relevant, IMHO.

I just did a quick grep of a random project - library primitives for the
most part, so I'd expect sizeof(type) to be more common in this code than
in most of my code.

In that project, which is about four thousand lines of ISO C, the sizeof
operator is used 37 times. Of those 37, just *one* takes the size of a
type. All the rest take the size of an expression.

The one that takes the size of a type is a macro, which represents the
number of bytes required for storing an object representation of an object
pointer - i.e. sizeof(void *).
 
S

Sarcastic Zombie

Richard said:
I have no idea if you are being a smartass there or not. I fail to see
the relation.

If I'm not mistaken...

The relation is that in this case, sizeof is essentually a plain old
operator. Thus sizeof(x) is the same thing as (x)++ or (j) + (k).

Parenthesis are used either for function calls, flow control or for
enforcing order of operations.

In this case it isn't a function and it isn't a flow control statement,
so all you're doing is enforcing the order of operations on a single
operation.

"We're doing one thing here, and whatever you do, make sure you do the
one thing we're doing first!"
 
G

goose

Lew said:
Richard said:
Frederick Gotham said:
Richard posted:

Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",


If "x" is a type, then the parentheses are mandatory:

int i = sizeof(int); /* OK */

int i = sizeof int; /* Compile Error */
[snip]
So at the end of the day "sizeof x" and "sizeof(x)" are identical?

Nope.

At the end of the day,
sizeof(x)
works under all conditions, and
sizeof x
only works if x is not a type

Yes, and thats a fact I tend to use to great
advantage: I simply omit the brackets out of habit.

Any error then flagged means that I have attempted
to use sizeof on a type, and that code is therefore
suspect and should be looked at more closely before
adding the braces (I very rarely, if ever, have a good
reason for taking the size of a type rather than
a variable).

Sorta like using casts - I omit them unless the
compiler complains about type mismatches, and then
I make *very* sure that a cast is really needed.

goose,
letting the compiler catch your errors.
 
S

Sarcastic Zombie

Parenthesis are used either for function calls, flow control or for
enforcing order of operations.

Oh, and typecasting! I forgot.

I only have a passing knowledge of C so forgive my ignorance, is the
reason we do sizeof (int) because we're doing some manner of
typecasting there?
 
C

Chris Dollin

Sarcastic said:
Oh, and typecasting! I forgot.

I only have a passing knowledge of C so forgive my ignorance, is the
reason we do sizeof (int) because we're doing some manner of
typecasting there?

No.

(It's just grammar.)
 
R

Richard

Sarcastic Zombie said:
If I'm not mistaken...

The relation is that in this case, sizeof is essentually a plain old
operator. Thus sizeof(x) is the same thing as (x)++ or (j) + (k).

its not "conceptually" the same in my view.

It makees sense to me to type it so:

x += sizeof(y);

maybe its because i'm getting old and am just more used to it.

I certainly wouldnt write any code of the form

x = (a) + (b);

And am still at a bit of a loss as to whyt Richard mentioned it when I
was asking if there are any differences between the two styles.
 
R

Richard

Chris Dollin said:
"Omit needless brackets".

I guess at the end of the day I "see" sizeof as a function - regardless
of the fact it isnt.

Traditionally all C code Ive ever worked on has always used parenthesis
in its use : it seems more natural to me. And now I know there is no
difference in the two styles I'll stick with it I think. Frankly I think

x=x+sizeof q - 3

is horrible and confusing.
 
F

Frederick Gotham

Richard posted:
It makees sense to me to type it so:

x += sizeof(y);


That makes "sizeof" look like a function rather than an operator. That's
misleading.
 
F

Frederick Gotham

Richard posted:
I guess at the end of the day I "see" sizeof as a function - regardless
of the fact it isnt.
Frankly I think

x=x+sizeof q - 3

is horrible and confusing.


It's horrible and confusing because your way of thinking is flawed. If you
think of "sizeof" as a function rather than as an operator, then you won't
understand:

int x;

sizeof x;
 
R

Richard

Frederick Gotham said:
Richard posted:




It's horrible and confusing because your way of thinking is flawed. If
you

All things that are confusing are confusing because the beholders
thinking is flawed.
think of "sizeof" as a function rather than as an operator, then you won't
understand:

int x;

sizeof x;

And when I first saw it this way I didnt : I had to look it up. But
regardless,

x=sizeof(y)+a+b;

is, for me, a lot more readable than

x=sizeof y+a+b;

But then I know you can parse gobbledygood in your sleep :-; I think
we've covered the issues now.

--
 
F

Frederick Gotham

Richard posted:
And when I first saw it this way I didnt : I had to look it up.


Well thankfully know you know that "sizeof" is a unary operator, not a
function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top