sizeof question

This is a discussion on sizeof question within the C++ Programming forums, part of the General Programming Boards category; When does sizeof require parentheses? I'm wondering because Code: struct foo { }; int main() { sizeof foo; } compiles ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    sizeof question

    When does sizeof require parentheses? I'm wondering because
    Code:
    struct foo {
    };
    
    int main()
    {
    	sizeof foo;
    }
    compiles in VC++ but not with g++ for me.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Why don't you just use parentheses all the time. It looks better, and makes sizeof look more like a function.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Curiously, a lot of people on this board don't use sizeof(something), they use sizeof something instead, so I'd like to think they are the same, but I'll agree that sizeof(something) looks better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You need parentheses when you're taking the sizeof a type. You can leave out the parentheses when you're taking the size of a named variable.

    In other words:
    Code:
    int i;
    sizeof(i);  // valid
    sizeof(int);  // valid
    sizeof i;  // valid
    sizeof int;  // invalid
    I'm not sure exactly why this is, but I think that some ambiguities in the grammar might have resulted if "sizeof int" were allowed.

    Personally, I always put parentheses in. Though I never put parentheses in for return statements. (Of course, that's different, because parentheses for return statements are always optional.) I guess it's just a matter of preference.

    [edit] As for the original example . . . it doesn't work with g++ because it isn't supposed to. "foo" is a type, so just as you can't use "sizeof int", you can't use "sizeof foo". If you declared a variable of type foo, however, you could take the sizeof that without parentheses.
    Code:
    struct foo {
    };
    
    int main()
    {
            foo f;
            sizeof f;
    }
    [/edit]
    Last edited by dwks; 06-21-2008 at 04:03 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    65
    So is VC++ not conforming to the standard?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Oh it's conforming to the standard, alright, but it does allow a lot of things other compilers bark out.
    Calling it smart, or calling bad, call it extensions, or whatever else wish.
    VC++ allows some things that aren't standard (but oh do I wish they were).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    No, VC++ is not conforming to standard C++ here. As far as I know, "sizeof type" is not allowed by the standard, and so MSVC should forbid it. (Perhaps some compatibility flag needs to be enabled?)

    "Conforming" to a standard not only requires allowing that which the standard allows, but also forbidding that which the standard disallows. For example, a C implementation that allows single line (//) comments is not, technically, conforming to C89.

    [edit] For example:
    Code:
    #include <iostream>
    
    int main() {
        struct name {} name;
        std::cout << sizeof name << std::endl;
    }
    If "sizeof type", without parentheses, is allowed, then what is that program to do? [/edit]
    Last edited by dwks; 06-21-2008 at 04:29 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Most likely due to language extensions being enabled by default.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    More evidence that the parentheses are required, from Wikipedia: http://en.wikipedia.org/wiki/Sizeof
    sizeof is a compile-time operator that returns the size, in multiples of the size of char, of the variable or parenthesized type-specifier that it precedes.
    More evidence from Google is ready for the reading: http://www.google.ca/search?hl=en&q=...G=Search&meta=

    On the other hand, cppreference.com gets it backwards. http://www.cppreference.com/keywords/sizeof.html
    The parentheses around the argument are not required if you are using sizeof with a variable type (e.g. sizeof(int)).
    [edit]
    Most likely due to language extensions being enabled by default.
    Perhaps. Can you enable strict ANSI mode? What happens then? [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    65
    Shouldn't VC++ at least give a warning for this? I remember doing something that compiled but still gave a "nonstandard extension used" warning. Also, how can I turn off langugage extensions in VC++?

    The cppreference.com reference is correct isn't it? Although the example doesn't seem to match.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    You can turn them off under project options, C/C++, Language.
    It warns about some extensions, but not all sadly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The cppreference.com reference is correct isn't it? Although the example doesn't seem to match.
    No, it has it backwards, as I stated.

    Observe.
    Code:
    $ nl -ba sizeofi.cpp
         1  #include <cstddef>
         2
         3  int main() {
         4      int i;
         5      size_t z;
         6      z = sizeof(i);  // valid
         7      z = sizeof(int);  // valid
         8      z = sizeof i;  // valid
         9      z = sizeof int;  // invalid
        10  }
    $ g++ sizeofi.cpp -o sizeofi
    sizeofi.cpp: In function ‘int main()’:
    sizeofi.cpp:9: error: expected primary-expression before ‘int’
    sizeofi.cpp:9: error: expected `;' before ‘int’
    $
    This clearly shows that "sizeof i" is valid and "sizeof int" is invalid, which is exactly the opposite of this statement:
    The parentheses around the argument are not required if you are using sizeof with a variable type (e.g. sizeof(int)).
    They are stating that "sizeof int" is valid. And implying that "sizeof i" is not. Oops.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    65
    I guess I interpreted "variable type" as just "variable". To me, it meant that parentheses are not required with variables, and the example included parentheses around int to emphasize that because int is not a variable, they were required. I thought it would've made more sense if the example used a variable without parentheses instead.

  14. #14
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38
    You shall use parentheses... or else you shall be damned to the fire abyss

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Section 5.3.3 of the 2003 edition of the C++ Standard states that: "The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is not evaluated, or a parenthesized type-id."

    Consequently, it is clear that sizeof x is allowed if x is an expression, but sizeof(x) is required if x is a type name.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read from 15-pin port
    By C_ntua in forum C Programming
    Replies: 23
    Last Post: 07-11-2008, 09:09 AM
  2. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  5. sizeof() EZ question
    By V1P3R V3N0M in forum Windows Programming
    Replies: 15
    Last Post: 01-11-2003, 10:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21