Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sizeof implementation

Reply
Thread Tools

sizeof implementation

 
 
Debajyoti Sarma
Guest
Posts: n/a
 
      07-11-2010
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.
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-11-2010
Debajyoti Sarma <(E-Mail Removed)> wrote:
> 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
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      07-11-2010
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
 
Reply With Quote
 
rudolf
Guest
Posts: n/a
 
      07-11-2010
In article <i1d47s$67k$(E-Mail Removed)>,
jacob navia <(E-Mail Removed)> wrote:

> 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.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      07-11-2010
rudolf a écrit :
> In article <i1d47s$67k$(E-Mail Removed)>,
> jacob navia <(E-Mail Removed)> wrote:
>
>> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-11-2010
Debajyoti Sarma <(E-Mail Removed)> writes:
> 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 news: You can't.
Good news: 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.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-11-2010
jacob navia <(E-Mail Removed)> writes:
> 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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      07-13-2010
Debajyoti Sarma <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      07-14-2010
Keith Thompson <(E-Mail Removed)> writes:

> jacob navia <(E-Mail Removed)> writes:
>> 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]);'.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An implementation where sizeof(short int) does not divide sizeof(int) Spiros Bousbouras C Programming 40 01-20-2007 09:58 PM
sizeof( int ) != sizeof( void * ) blufox C Programming 2 05-22-2006 03:25 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments