sizeof operator implementation

Discussion in 'C Programming' started by srivatsan_b, May 1, 2005.

  1. srivatsan_b

    srivatsan_b Guest

    Can anybody please explain how sizeof operator is implemented?

    Thanks in advance
    Srivatsan B
     
    srivatsan_b, May 1, 2005
    #1
    1. Advertising

  2. srivatsan_b wrote on 01/05/05 :
    > Can anybody please explain how sizeof operator is implemented?


    No. Implementation is not part of the C language. The standard only
    describes the behaviour and the interface of this operator.

    "Returns the size of a (type) or of an object expressed in bytes. The
    returned type is size_t."

    Why do you want to know these detalls ?

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    ..sig under repair
     
    Emmanuel Delahaye, May 1, 2005
    #2
    1. Advertising

  3. srivatsan_b

    Malcolm Guest

    "srivatsan_b" <> wrote
    > Can anybody please explain how sizeof operator is implemented?
    >

    In traditional C it looks like a function but isn't. sizeof(int) resolves to
    a compile time constant, which the compiler cna do because it knows the
    number of bytes taken for each type.
    (You cannot portably replace with a constant in code because int may be 2, 4
    or sometimes even a different number of bytes on different machines).
     
    Malcolm, May 1, 2005
    #3
  4. On Sun, 01 May 2005 00:08:34 -0700, srivatsan_b wrote:

    > Can anybody please explain how sizeof operator is implemented?
    >
    > Thanks in advance
    > Srivatsan B


    As you say sizeof is an operator i.e. it is built into the compiler just
    like + or >>

    The sizeof operator evaluates to the size in bytes of the type of its
    operand. Since the compiler knows the size of any type it supports it
    simply determines the type and then the size of that type.

    Lawrence
     
    Lawrence Kirby, May 1, 2005
    #4
  5. <posted & mailed>

    srivatsan_b wrote:

    > Can anybody please explain how sizeof operator is implemented?


    Since it's implementation is dependent on the compiler, ask the compiler's
    author how he did it.


    >
    > Thanks in advance
    > Srivatsan B


    --
    Remove '.nospam' from e-mail address to reply by e-mail
     
    James McIninch, May 1, 2005
    #5
  6. "Malcolm" <> writes:
    > "srivatsan_b" <> wrote
    >> Can anybody please explain how sizeof operator is implemented?
    >>

    > In traditional C it looks like a function but isn't. sizeof(int) resolves to
    > a compile time constant, which the compiler cna do because it knows the
    > number of bytes taken for each type.
    > (You cannot portably replace with a constant in code because int may be 2, 4
    > or sometimes even a different number of bytes on different machines).


    It can look like a function call if you use parentheses, but it's
    actually an operator. For example, given "int x;", the expression to
    compute the size of x is "sizeof x" (you can use "sizeof(x)", but the
    parentheses are superfluous). The parentheses are required if the
    argument is a type name, as in "sizeof(int)", but of course a function
    can't take a type name as an argument.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 1, 2005
    #6
    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. Manish_Ganvir
    Replies:
    13
    Views:
    1,599
    Keith Thompson
    Feb 14, 2005
  2. Aloo

    implementation of the 'sizeof' operator

    Aloo, Sep 7, 2005, in forum: C Programming
    Replies:
    5
    Views:
    702
    Keith Thompson
    Sep 7, 2005
  3. vijay

    sizeof operator implementation

    vijay, Dec 7, 2006, in forum: C Programming
    Replies:
    4
    Views:
    4,457
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
    Dec 8, 2006
  4. Spiros Bousbouras
    Replies:
    40
    Views:
    1,500
    Keith Thompson
    Jan 20, 2007
  5. Kislay

    The sizeof operator : sizeof(++i)

    Kislay, Oct 18, 2007, in forum: C Programming
    Replies:
    10
    Views:
    699
    Peter Pichler
    Oct 19, 2007
Loading...

Share This Page