sizeof of expression & sizeof of type

Discussion in 'C++' started by Alex Vinokur, Jun 10, 2006.

  1. Alex Vinokur

    Alex Vinokur Guest

    Why does one need to use two kinds of sizeof operator:
    * sizeof unary-expression,
    * sizeof (type-name)
    ?

    Their behavior seem not to be different (see an example below).

    ------ C++ code ------
    #include <iostream>
    using namespace std;

    int main()
    {
    int x = 100;

    cout << sizeof ++x << endl; // expression sizeof
    cout << x << endl;

    x = 100;
    cout << endl;
    cout << sizeof (++x) << endl; // type sizeof
    cout << x << endl;

    return 0;
    }
    ----------------------

    ------ Run ------

    4
    100

    4
    100

    -----------------

    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Jun 10, 2006
    #1
    1. Advertising

  2. Alex Vinokur

    Rolf Magnus Guest

    Alex Vinokur wrote:

    > Why does one need to use two kinds of sizeof operator:
    > * sizeof unary-expression,
    > * sizeof (type-name)
    > ?


    Because you might want to use it on a value to find out its size, or you
    want to use it on a type to find out its size.

    >
    > Their behavior seem not to be different (see an example below).
    >
    > ------ C++ code ------
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > int x = 100;
    >
    > cout << sizeof ++x << endl; // expression sizeof
    > cout << x << endl;
    >
    > x = 100;
    > cout << endl;
    > cout << sizeof (++x) << endl; // type sizeof


    (++x) is not a type. int would be a type.

    > cout << x << endl;
    >
    > return 0;
    > }
    > ----------------------
    >
    > ------ Run ------
    >
    > 4
    > 100
    >
    > 4
    > 100
     
    Rolf Magnus, Jun 10, 2006
    #2
    1. Advertising

  3. Alex Vinokur

    Alex Vinokur Guest

    "Rolf Magnus" <> wrote in message news:e6eihq$66v$03$-online.com...
    > Alex Vinokur wrote:

    [snip]
    > >
    > > int main()
    > > {
    > > int x = 100;
    > >
    > > cout << sizeof ++x << endl; // expression sizeof
    > > cout << x << endl;
    > >
    > > x = 100;
    > > cout << endl;
    > > cout << sizeof (++x) << endl; // type sizeof

    >
    > (++x) is not a type. int would be a type.

    [snip]

    I think, ++x is parsed as typename in sizeof (++x).


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Jun 10, 2006
    #3
  4. Alex Vinokur wrote:
    > "Rolf Magnus" <> wrote in message news:e6eihq$66v$03$-online.com...
    > > Alex Vinokur wrote:

    > [snip]
    > > >
    > > > int main()
    > > > {
    > > > int x = 100;
    > > >
    > > > cout << sizeof ++x << endl; // expression sizeof
    > > > cout << x << endl;
    > > >
    > > > x = 100;
    > > > cout << endl;
    > > > cout << sizeof (++x) << endl; // type sizeof

    > >
    > > (++x) is not a type. int would be a type.

    > [snip]
    >
    > I think, ++x is parsed as typename in sizeof (++x).


    No it is a unary-expression because a primary-expression is also a
    unary-expression and ( expression ) is a primary-expression and clearly
    ++x is an expression.
     
    Markus Schoder, Jun 10, 2006
    #4
  5. Alex Vinokur

    Alex Vinokur Guest

    Markus Schoder wrote:
    > Alex Vinokur wrote:
    > > "Rolf Magnus" <> wrote in message news:e6eihq$66v$03$-online.com...
    > > > Alex Vinokur wrote:

    > > [snip]
    > > > >
    > > > > int main()
    > > > > {
    > > > > int x = 100;
    > > > >
    > > > > cout << sizeof ++x << endl; // expression sizeof
    > > > > cout << x << endl;
    > > > >
    > > > > x = 100;
    > > > > cout << endl;
    > > > > cout << sizeof (++x) << endl; // type sizeof
    > > >
    > > > (++x) is not a type. int would be a type.

    > > [snip]
    > >
    > > I think, ++x is parsed as typename in sizeof (++x).

    >
    > No it is a unary-expression because a primary-expression is also a
    > unary-expression and ( expression ) is a primary-expression and clearly
    > ++x is an expression.


    What is the difference between
    sizeof ++x
    and
    sizeof (++x)
    ?

    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Jun 10, 2006
    #5
  6. Alex Vinokur

    Phlip Guest

    Alex Vinokur wrote:

    > What is the difference between
    > sizeof ++x
    > and
    > sizeof (++x)
    > ?


    Nothing. sizeof takes an expression, and parens in expressions may be
    optional.

    sizeof(int) is the size of the typecast to int. (int)0.

    --
    Phlip
    http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
     
    Phlip, Jun 10, 2006
    #6
  7. Alex Vinokur

    Alex Vinokur Guest

    Phlip wrote:
    > Alex Vinokur wrote:
    >
    > > What is the difference between
    > > sizeof ++x
    > > and
    > > sizeof (++x)
    > > ?

    >
    > Nothing. sizeof takes an expression, and parens in expressions may be
    > optional.


    So, why do we need two kinds of sizeof operator?

    >
    > sizeof(int) is the size of the typecast to int. (int)0.


    [snip]

    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Jun 10, 2006
    #7
  8. Alex Vinokur wrote:
    > Phlip wrote:
    > > Alex Vinokur wrote:
    > >
    > > > What is the difference between
    > > > sizeof ++x
    > > > and
    > > > sizeof (++x)
    > > > ?

    > >
    > > Nothing. sizeof takes an expression, and parens in expressions may be
    > > optional.

    >
    > So, why do we need two kinds of sizeof operator?


    One takes an expression and returns the size of the expression's type
    and one takes a type directly (which must be put in parentheses). It is
    mostly a matter of convenience.
     
    Markus Schoder, Jun 10, 2006
    #8
  9. Alex Vinokur schrieb:
    > Phlip wrote:
    >> Alex Vinokur wrote:
    >>
    >>> What is the difference between
    >>> sizeof ++x
    >>> and
    >>> sizeof (++x)
    >>> ?

    >> Nothing. sizeof takes an expression, and parens in expressions may be
    >> optional.

    >
    > So, why do we need two kinds of sizeof operator?


    int x;

    1) sizeof(int)
    2) sizeof(x) (or sizeof x)

    Which one do you think is redundant?

    Thomas
     
    Thomas J. Gritzan, Jun 11, 2006
    #9
  10. Alex Vinokur

    Alex Vinokur Guest

    Alex Vinokur, Jun 11, 2006
    #10
  11. Alex Vinokur

    Default User Guest

    Alex Vinokur wrote:

    >
    > Thomas J. Gritzan wrote:


    > > Which one do you think is redundant?
    > >

    >
    > 1) sizeof(int)
    > 2) sizeof(x)
    > 3) sizeof x // redundant


    You are confused. 2 and 3 are the same thing, there just are
    superfluous parentheses in 2. It's somewhat similar, although not
    exactly, to this:


    return (2);

    return 2;



    Those aren't redundant forms of return, just unneeded parentheses.




    Brian
     
    Default User, Jun 11, 2006
    #11
  12. Alex Vinokur

    Steve Pope Guest

    Default User <> wrote:

    >Alex Vinokur wrote:


    >> Thomas J. Gritzan wrote:


    >> > Which one do you think is redundant?


    >> 1) sizeof(int)
    >> 2) sizeof(x)
    >> 3) sizeof x // redundant


    >You are confused. 2 and 3 are the same thing, there just are
    >superfluous parentheses in 2. It's somewhat similar, although not
    >exactly, to this:


    >return (2);


    >return 2;


    Not exactly indeed, due to operator precedence.

    int x;
    long long y;
    int z = sizeof x, y;

    Gives you 4, while

    int x=1;
    int y=2;
    int z = return 1, 2;

    Gives you 2.

    Steve
     
    Steve Pope, Jun 12, 2006
    #12
  13. Alex Vinokur

    Default User Guest

    Steve Pope wrote:

    > Default User <> wrote:
    >
    > > Alex Vinokur wrote:

    >
    > >> Thomas J. Gritzan wrote:

    >
    > >> > Which one do you think is redundant?

    >
    > >> 1) sizeof(int)
    > >> 2) sizeof(x)
    > >> 3) sizeof x // redundant

    >
    > > You are confused. 2 and 3 are the same thing, there just are
    > > superfluous parentheses in 2. It's somewhat similar, although not
    > > exactly, to this:

    >
    > > return (2);

    >
    > > return 2;

    >
    > Not exactly indeed, due to operator precedence.
    >
    > int x;
    > long long y;
    > int z = sizeof x, y;
    >
    > Gives you 4, while
    >
    > int x=1;
    > int y=2;
    > int z = return 1, 2;
    >
    > Gives you 2.


    What does any of that have to do with parentheses? The OP didn't
    mention the comma operator, nor did I.



    Brian
     
    Default User, Jun 12, 2006
    #13
  14. Alex Vinokur

    Steve Pope Guest

    Default User <> wrote:

    >Steve Pope wrote:


    >> Default User <> wrote:


    >> > Alex Vinokur wrote:

    >>
    >> >> Thomas J. Gritzan wrote:

    >>
    >> >> > Which one do you think is redundant?

    >>
    >> >> 1) sizeof(int)
    >> >> 2) sizeof(x)
    >> >> 3) sizeof x // redundant

    >>
    >> > You are confused. 2 and 3 are the same thing, there just are
    >> > superfluous parentheses in 2. It's somewhat similar, although not
    >> > exactly, to this:

    >>
    >> > return (2);

    >>
    >> > return 2;


    >> Not exactly indeed, due to operator precedence.
    >>
    >> int x;
    >> long long y;
    >> int z = sizeof x, y;
    >>
    >> Gives you 4, while
    >>
    >> int x=1;
    >> int y=2;
    >> int z = return 1, 2;
    >>
    >> Gives you 2.


    >What does any of that have to do with parentheses? The OP didn't
    >mention the comma operator, nor did I.


    The "sizeof" operator has a certain precedence. (Look at
    the table of operator precedences e.g. in Stroustrup.) The "return"
    statement is not an operator, therefore has lower precedence
    than any operator, therefore the entire expression to the right
    of it is evaluated first.

    That's how I look at it, in any case.

    Steve
     
    Steve Pope, Jun 12, 2006
    #14
  15. Alex Vinokur

    tedu Guest

    Alex Vinokur wrote:
    > Thomas J. Gritzan wrote:
    > [snip]
    > >
    > > int x;
    > >
    > > 1) sizeof(int)
    > > 2) sizeof(x) (or sizeof x)
    > >
    > > Which one do you think is redundant?
    > >

    >
    > 1) sizeof(int)
    > 2) sizeof(x)
    > 3) sizeof x // redundant


    you forgot
    4) sizeof ((x))
    5) sizeof (((x)))
    ....
     
    tedu, Jun 12, 2006
    #15
  16. Alex Vinokur

    Default User Guest

    Steve Pope wrote:

    > Default User <> wrote:


    > > What does any of that have to do with parentheses? The OP didn't
    > > mention the comma operator, nor did I.

    >
    > The "sizeof" operator has a certain precedence.


    > That's how I look at it, in any case.


    Hence the qualifier, "It's somewhat similar, although not exactly, to
    this". Yeah, we can construct some examples where the () could matter,
    but they don't for the ones the OP presented.



    Brian
     
    Default User, Jun 12, 2006
    #16
    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,391
    Ron Natalie
    Oct 14, 2004
  2. Trevor

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

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

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    573
    Joe Smith
    May 22, 2006
  5. Yevgen Muntyan

    #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type)))

    Yevgen Muntyan, Feb 9, 2007, in forum: C Programming
    Replies:
    10
    Views:
    931
    Yevgen Muntyan
    Feb 13, 2007
Loading...

Share This Page