[Newbie] What is sizeof() ?

V

Vincent Pierroux

Hi,

I search the header file of sizeof() function but i don't find.
Where can find that ?
Sizeof() is a function ? macro ? "os function" ?
I don't understand what is exactly sizeof.

Vincent Pierroux
 
E

Eric

Vincent Pierroux said:
Hi,

I search the header file of sizeof() function but i don't find.
Where can find that ?
Sizeof() is a function ? macro ? "os function" ?
I don't understand what is exactly sizeof.

It's an operator.

You might find it useful to read the questions in the C FAQ reguarding
the sizeof operator at:

http://www.eskimo.com/~scs/C-faq/questions.html

Even though sizeof is an operator, I will still write my code as sizeof(
.... ) because I find it easier to read that way.
 
J

Jeremy Yallop

Vincent said:
Sizeof() is a function ? macro ? "os function" ?

It's an operator.
I don't understand what is exactly sizeof.

The sizeof operator yields the size in bytes of its operand, which may
be an expression or a parenthesized type name.

Jeremy.
 
D

Dave Vandervies

Hi,

I search the header file of sizeof() function but i don't find.
Where can find that ?
Sizeof() is a function ? macro ? "os function" ?
I don't understand what is exactly sizeof.

sizeof is an operator. It's built into the language, not a macro or
function, so you won't find it in your header files. Your textbook
should mention it somewhere, with more details.

Given an expression, it returns a size_t representing the size (in bytes)
of a variable with the type of that expression. Given a parenthesized
type, it returns the size of a variable with that type.


dave
 
V

Vincent Pierroux

Thanks for your answer.

So, if it is a operator i don't have any argument push on the stack like a
function.

If i make onething like that

for(foo=0;foo<1000;i++)
foo+= sizeof myStruct ;

it take the same processing time this

var= sizeof myStruct
for(foo=0;foo<1000;i++)
foo+= var;

(it is a stupid code but it is for the sample ;) )

Vincent Pierroux
 
M

Mark A. Odell

Thanks for your answer.

So, if it is a operator i don't have any argument push on the stack like
a function.

Yeah, I guess.
If i make onething like that

for(foo=0;foo<1000;i++)
foo+= sizeof myStruct ;

it take the same processing time this

var= sizeof myStruct
for(foo=0;foo<1000;i++)
foo+= var;

That would depend upon your compiler but they'd be pretty similar. Here's
a another usage of sizeof:

....

int idx;
float arr[1000];

....

for (idx = 0; idx < sizeof fload / sizeof *float; ++idx)
{
/* work */
}

Sometimes I make a macro of this:

#define NUM_OF(x) (sizeof (x) / sizeof *(x))

No function call overhead whatsoever.
 
D

Dave Vandervies

If you can read this, you are a nimis argutior, right?

uhmm... something like that, I think, depending on what you mean by "a
nimis argutior", especially if you mean "an excessively educated person".

I got that one from a coworker's tshirt, and she translated it for me as
"If you can read this, you are far too educated".


dave

--
Dave Vandervies (e-mail address removed)
May I be excused for showing incomplete/incorrect knowledge of C++ knowledge
in a C newsgroup? --Michael Rubenstein and Ian Woods in comp.lang.c
 
J

Joona I Palaste

uhmm... something like that, I think, depending on what you mean by "a
nimis argutior", especially if you mean "an excessively educated person".
I got that one from a coworker's tshirt, and she translated it for me as
"If you can read this, you are far too educated".

I prefer the name of a level in "Pogo Joe":
IF U CN RD THS, U R WRD!

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You can pick your friends, you can pick your nose, but you can't pick your
relatives."
- MAD Magazine
 
I

Irrwahn Grausewitz

That would depend upon your compiler but they'd be pretty similar. Here's
a another usage of sizeof:
...
int idx;
float arr[1000];
...
for (idx = 0; idx < sizeof fload / sizeof *float; ++idx)
^^^^^ ^^^^^^
Err, ... I mean, what???
ITYM:
for (idx = 0; idx < sizeof arr / sizeof *arr; ++idx)

{
/* work */
}

<snip>
 
M

Mark A. Odell

That would depend upon your compiler but they'd be pretty similar.
Here's a another usage of sizeof:
...
int idx;
float arr[1000];
...
for (idx = 0; idx < sizeof fload / sizeof *float; ++idx)
^^^^^ ^^^^^^
Err, ... I mean, what???
^^^ ^^^^
Sorry, I meant: ||| ||||
arr *arr
 
P

Peter Pichler

Vincent Pierroux said:
So, if it is a operator i don't have any argument push on the stack like a
function.

If i make onething like that

for(foo=0;foo<1000;i++)
foo+= sizeof myStruct ;

it take the same processing time this

var= sizeof myStruct
for(foo=0;foo<1000;i++)
foo+= var;

Most likely the first one will take shorter: sizeof myStruct is a constant,
so the compiler might just replace the whole loop with

foo += 1000 * sizeof myStruct;

On the other hand, so it could in the second case, should it be clever
enough.
 
C

Christian Bau

"Peter Pichler said:
Most likely the first one will take shorter: sizeof myStruct is a constant,
so the compiler might just replace the whole loop with

foo += 1000 * sizeof myStruct;

On the other hand, so it could in the second case, should it be clever
enough.

You mean stupid enough? Take a very good look at the for loop. It is
most likely _not_ executed 1000 times.
 
P

Peter Pichler

Christian Bau said:
You mean stupid enough? Take a very good look at the for loop. It is
most likely _not_ executed 1000 times.

Doh! You are right, of course. I fell for "do what I want, not what I say"
:)
 
M

Martin Ambuhl

Vincent said:
Hi,

I search the header file of sizeof() function

It is not a function, but an operator.
but i don't find.
Where can find that ?
Sizeof() is a function ? macro ? "os function" ?

No, it is not a function, macro, or os function. 'sizeof' is an operator,
as are such things as '/', '+', '&', and '||'.
 
V

Vittal

Vincent Pierroux said:
Hi,

I search the header file of sizeof() function but i don't find.
Where can find that ?
Sizeof() is a function ? macro ? "os function" ?
I don't understand what is exactly sizeof.

Vincent Pierroux

Niether its a function nor its a macro. Its an operator, which returns
the size of a perticular data type.
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top