How sizeof() works?

M

madhura_khot

Hi All,
I want to know how sizeof() works? Whether it calculates size of the
object at run time or at compile time?

Thanks,
Madhura
 
R

Richard Heathfield

(e-mail address removed) said:
Hi All,
I want to know how sizeof() works? Whether it calculates size of the
object at run time or at compile time?

At compile time. The only exception occurs in C99-conforming compilers
(precious few of those, but apparently there are at least some around)
where variable-length arrays are concerned.
 
M

madhura_khot

But how it calculates the size of object. I mean how it came to know
about a class structure if we use sizeof() to calculate the size of an
user definied class object?
 
R

Richard Tobin

But how it calculates the size of object. I mean how it came to know
about a class structure if we use sizeof() to calculate the size of an
user definied class object?

C doesn't have classes.

Do you means structs? If so, it knows the size because it knows the
definition of the struct.

-- Richard
 
P

pemo

But how it calculates the size of object. I mean how it came to know
about a class structure if we use sizeof() to calculate the size of an
user definied class object?

C doesn't use classes - if you've a C++ question, ask it on comp.lang.c++

A C compiler obviously has to know how big objects are, e.g.,

struct thing
{
char c[100];
int n;
};

int main(void)
{
struct thing t;
...

Obviously, the correct amount of space for the auto t has to be allocated -
the compiler's 'seen' struct thing previously, and knows how big it is - so
it can allocate the space ok. So, when you sizeof t, or sizeof struct
thing, you're simply asking the compiler to tell you what it's decided about
the size of an object it already knows about, and to replace sizeof t with a
number which equates to the calculated size.
 
J

Jordan Abel

But how it calculates the size of object. I mean how it came to know
about a class structure if we use sizeof() to calculate the size of an
user definied class object?

No such thing - but if you use it on a struct object, it's whatever size
the compiler decided to make the struct. for example,

struct x {
int a;
char b;
};

sizeof(struct x) could be 8, 5, 4, or 3. [or any other number it wants
it to be, but those four are reasonable on 32- or 16-bit systems.]
 
C

Chuck F.

But how it calculates the size of object. I mean how it came to
know about a class structure if we use sizeof() to calculate the
size of an user definied class object?

There are no classes in C. You probably want c.l.c++.

Always include suitable context in your replies. See below for how
to do it on the broken google interface. Please read the
referenced URL before posting again.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
U

Ulrich Eckhardt

I want to know how sizeof() works? Whether it calculates size of the
object at run time or at compile time?

Compile time - I don't know about C99 and its variable size arrays but
trust what Richard said about them.

Anyhow, in case that is your confusion, 'sizeof' is not a function but an
operator. There are two arguments to this unary operator, an object or a
type. Example:

int i;
size_t s1 = sizeof i;
size_t s2 = sizeof (int);
assert(s1==s2);

As you see, the brackets aren't necessary in the first case.

Uli
 
E

Emmanuel Delahaye

(e-mail address removed) a écrit :
I want to know how sizeof() works? Whether it calculates size of the
object at run time or at compile time?

Like your C-book said: at compile-time[1]
 
E

Emmanuel Delahaye

(e-mail address removed) a écrit :
But how it calculates the size of object. I mean how it came to know
about a class structure if we use sizeof() to calculate the size of an
user definied class object?
The magic comes from the type. It gives the size. It is so true that the
type itself, when properly enclosed in parens, give it's own size :

size_t size_of_an_int = sizeof (int);
 
E

Emmanuel Delahaye

(e-mail address removed) a écrit :
But how it calculates the size of object. I mean how it came to know
about a class structure if we use sizeof() to calculate the size of an
user definied class object?
The magic comes from the type. It gives the size. It is so true that the
type itself, when properly enclosed in parens, gives its own size :

size_t size_of_an_int = sizeof (int);
 
M

Mark McIntyre

struct x {
int a;
char b;
};

sizeof(struct x) could be 8, 5, 4, or 3. [or any other number it wants
it to be,

with the exception of one or zero, in the above case.

Mark McIntyre
 

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
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top