sizeof implementation

Discussion in 'C Programming' started by Debajyoti Sarma, Jul 11, 2010.

  1. 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.
     
    Debajyoti Sarma, Jul 11, 2010
    #1
    1. Advertising

  2. Debajyoti Sarma <> 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://toerring.de
     
    Jens Thoms Toerring, Jul 11, 2010
    #2
    1. Advertising

  3. Debajyoti Sarma

    jacob navia Guest

    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
     
    jacob navia, Jul 11, 2010
    #3
  4. Debajyoti Sarma

    rudolf Guest

    In article <i1d47s$67k$>,
    jacob navia <> 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.
     
    rudolf, Jul 11, 2010
    #4
  5. Debajyoti Sarma

    jacob navia Guest

    rudolf a écrit :
    > In article <i1d47s$67k$>,
    > jacob navia <> 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
     
    jacob navia, Jul 11, 2010
    #5
  6. Debajyoti Sarma <> 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) <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"
     
    Keith Thompson, Jul 11, 2010
    #6
  7. jacob navia <> 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) <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"
     
    Keith Thompson, Jul 11, 2010
    #7
  8. Debajyoti Sarma

    Richard Bos Guest

    Debajyoti Sarma <> 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
     
    Richard Bos, Jul 13, 2010
    #8
  9. Debajyoti Sarma

    Tim Rentsch Guest

    Keith Thompson <> writes:

    > jacob navia <> 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]);'.
     
    Tim Rentsch, Jul 14, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,338
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    633
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,426
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    560
    Joe Smith
    May 22, 2006
  5. Spiros Bousbouras
    Replies:
    40
    Views:
    1,485
    Keith Thompson
    Jan 20, 2007
Loading...

Share This Page