sizeof implementation

D

Debajyoti Sarma

How to implement sizeof operator in C ?
cases
1. int i=0; sizeof(i)=4 ...i.e size of variavles
2. sizeof(int)=4 ...i.e size of datatypes
3. struct t
{
int a;
char c;
}p;
sizeof(p)=5 ....i.e it should work for user
define data types
sizeof(t)=5

Function is more preferable than macro.
 
J

Jens Thoms Toerring

Debajyoti Sarma said:
How to implement sizeof operator in C ?

You can't (except if you write you own C compiler that you
write in C and you know all the details of the underlying
hardware).
cases
1. int i=0; sizeof(i)=4 ...i.e size of variavles
2. sizeof(int)=4 ...i.e size of datatypes

sizeof(int) is 4 on some implementions, on others it can have a
rather different value.
3. struct t
{
int a;
char c;
}p;
sizeof(p)=5 ....i.e it should work for user

Even if sizeof(int) is 4 on your system you can't count on a
structure with an int and a char to have a size of 5. The
compiler may very well insert padding bytes.
define data types
sizeof(t)=5
Function is more preferable than macro.

What are you talking about? 'sizeof' is an operator and neither
a function nor a macro. Moreover, it's evaluated at compile time
so it can't be replaced by a function (which only can be executed
at run time).
Regards, Jens
 
J

jacob navia

Jens Thoms Toerring a écrit :
What are you talking about? 'sizeof' is an operator and neither
a function nor a macro. Moreover, it's evaluated at compile time
so it can't be replaced by a function (which only can be executed
at run time).

In C99 that is not true

size_t fn(int a)
{
int tab[a];

size_t s = sizeof(a);
return s;
}

This is evaluated at run time. In general you are correct, only this specific case
is different
 
R

rudolf

jacob navia said:
Jens Thoms Toerring a écrit :
What are you talking about? 'sizeof' is an operator and neither
a function nor a macro. Moreover, it's evaluated at compile time
so it can't be replaced by a function (which only can be executed
at run time).

In C99 that is not true

size_t fn(int a)
{
int tab[a];

size_t s = sizeof(a);
return s;
}

This is evaluated at run time. In general you are correct, only this specific
case
is different

I think you made a typo.

sizeof(a) should be evaluated at compile time (why would it not be?)

sizeof(tab) would be evaluated at run time.
 
J

jacob navia

rudolf a écrit :
jacob navia said:
Jens Thoms Toerring a écrit :
What are you talking about? 'sizeof' is an operator and neither
a function nor a macro. Moreover, it's evaluated at compile time
so it can't be replaced by a function (which only can be executed
at run time).
In C99 that is not true

size_t fn(int a)
{
int tab[a];

size_t s = sizeof(a);
return s;
}

This is evaluated at run time. In general you are correct, only this specific
case
is different

I think you made a typo.

sizeof(a) should be evaluated at compile time (why would it not be?)

sizeof(tab) would be evaluated at run time.

Yes, obviously

Thanks for the correction :)

jacob
 
K

Keith Thompson

Debajyoti Sarma said:
How to implement sizeof operator in C ?
cases
1. int i=0; sizeof(i)=4 ...i.e size of variavles
2. sizeof(int)=4 ...i.e size of datatypes
3. struct t
{
int a;
char c;
}p;
sizeof(p)=5 ....i.e it should work for user
define data types
sizeof(t)=5

Function is more preferable than macro.

Bad You can't.
Good You don't need to.
Question: Why would you want to?

You don't need to implement sizeof in C; it's an operator that's
built into the language, just like "+" and "=".

It's not possible to re-implement sizeof as a function.

There is a trick that lets you re-implement sizeof for objects,
using a macro that takes advantage of the properties of pointer
arithmetic. This trick doesn't work for expressions in general, nor
does it work for types. I'm deliberately not showing the trick.
(Note that "sizeof expr" and "sizeof ( type )" are really two
different kinds of expression.)
 
K

Keith Thompson

jacob navia said:
Jens Thoms Toerring a écrit :
What are you talking about? 'sizeof' is an operator and neither
a function nor a macro. Moreover, it's evaluated at compile time
so it can't be replaced by a function (which only can be executed
at run time).

In C99 that is not true

size_t fn(int a)
{
int tab[a];

size_t s = sizeof(a);
return s;
}

This is evaluated at run time. In general you are correct, only this
specific case is different

As mentioned downthread, you meant sizeof(tab), not sizeof(a). And you
don't really need the intermediate object:

size_t fn(int a)
{
int tab[a];
return sizeof tab;
}

But surely a replacement for sizeof would return the size of its
argument. Your function, with the correction, is merely equivalent to:

size_t fn(int a)
{
return a * sizeof(int);
}

except that it can fail (with undefined behavior) if tab can't be
allocated.
 
R

Richard Bos

Debajyoti Sarma said:
How to implement sizeof operator in C ?

One cannot. It is already built-in, and its dual nature means that it
cannot be implemented in a single function/macro/whatever written in C
itself.
Function is more preferable than macro.

No, it isn't; it's an operator, not a function _or_ a macro.

There is a reason why sizeof is built-in. You should not try to do what
has already been done for you.

Richard
 
T

Tim Rentsch

Keith Thompson said:
jacob navia said:
Jens Thoms Toerring a @C3{A9}crit :
What are you talking about? 'sizeof' is an operator and neither
a function nor a macro. Moreover, it's evaluated at compile time
so it can't be replaced by a function (which only can be executed
at run time).

In C99 that is not true

size_t fn(int a)
{
int tab[a];

size_t s = sizeof(a);
return s;
}

This is evaluated at run time. In general you are correct, only this
specific case is different

As mentioned downthread, you meant sizeof(tab), not sizeof(a). And you
don't really need the intermediate object:

size_t fn(int a)
{
int tab[a];
return sizeof tab;
}

Nor 'tab' either; just 'return sizeof (int[a]);'.
 

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
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top