sizeof member - c++

This is a discussion on sizeof member - c++ ; Hello guys! I've a simple question. Is sizeof expression in following code snippet valid? struct SomeStructure { char some_member[10]; }; const size_t size = sizeof(SomeStructure::some_member); Some compilers passes it without errors and give valid results (e.g., IBM Visual Age). -- ...

+ Reply to Thread
Results 1 to 5 of 5

sizeof member

  1. Default sizeof member

    Hello guys!
    I've a simple question.

    Is sizeof expression in following code snippet valid?

    struct SomeStructure
    {
    char some_member[10];
    };
    const size_t size = sizeof(SomeStructure::some_member);

    Some compilers passes it without errors and give valid results (e.g.,
    IBM Visual Age).

    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  2. Default Re: sizeof member

    On 5 Okt., 22:17, "Alexander Arhipenko" <arhip...@gmail.com> wrote:
    > Is sizeof expression in following code snippet valid?
    >
    > struct SomeStructure
    > {
    > char some_member[10];};
    >
    > const size_t size = sizeof(SomeStructure::some_member);
    >
    > Some compilers passes it without errors and give valid results (e.g.,
    > IBM Visual Age).


    Currently the above expression is not not supported by
    Our Holy Standard. But note that there exists a proposal
    to fix exactly this:

    http://www.open-std.org/jtc1/sc22/wg...007/n2253.html

    Also note that the current draft of upcoming C++0x does
    already contain the proposed wording changes.

    Greetings from Bremen,

    Daniel Krügler



    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  3. Default Re: sizeof member

    On 6 Oct, 00:17, "Alexander Arhipenko" <arhip...@gmail.com> wrote:
    > Is sizeof expression in following code snippet valid?
    >
    > struct SomeStructure
    > {
    > char some_member[10];};
    >
    > const size_t size = sizeof(SomeStructure::some_member);


    No, it is not. 5.1[expr.prim]/10 explicitly lists the cases in which
    an id-expression (SomeStructure::some_member in this case) for
    nonstatic member functions is legal. These are:

    1. On the right side of operator. or operator->, for qualified member
    access
    2. As operand of unary &, to create function pointers
    3. To invoke members of base classes in constructors nonstatic member
    functions of a derived class

    Any other possible use, including argument of sizeof, is not allowed.
    The usual trick to do what you want is to do some cast trickery:

    const size_t size = sizeof(static_cast<SomeStructure*>(0)-
    >some_member);


    Note that, since argument of sizeof is not evaluated, dereferencing a
    null pointer is not a problem in this case.


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  4. Default Re: sizeof member

    > But note that there exists a proposal to fix exactly this:
    >
    > http://www.open-std.org/jtc1/sc22/wg...007/n2253.html
    >


    Interesting. That proposal has an example which has

    int i = sizeof(S::m); // ok
    int j = sizeof(S::m + 42); // error: reference to non-static member in
    subexpression

    Surely that should be

    size_t i = sizeof(S::m); // ok
    size_t j = sizeof(S::m + 42); // error: reference to non-static member in
    subexpression

    Stephen Howe


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  5. Default Re: sizeof member

    On Oct 18, 2:40 am, "Stephen Howe"
    <sjhoweATdialDOTpipexDOT...@giganews.com> wrote:
    > > But note that there exists a proposal to fix exactly this:

    >
    > >http://www.open-std.org/jtc1/sc22/wg...007/n2253.html

    >
    > Interesting. That proposal has an example which has
    >
    > int i = sizeof(S::m); // ok
    > int j = sizeof(S::m + 42); // error: reference to non-static member in
    > subexpression
    >
    > Surely that should be
    >
    > size_t i = sizeof(S::m); // ok
    > size_t j = sizeof(S::m + 42); // error: reference to non-static member in
    > subexpression


    No, a C++ program should favor "int" variables for storing integral
    values - unless there is a reason for using a different type. In this
    example, there is no doubt the the result of the sizeof expression
    will fit in an int. Whereas, a size_t variable would likely be less
    efficient than an int - by likely being larger (and much larger than
    is needed here). A size_t type would have the additional disadvantage
    of being unsigned (a fact that the "size_t" typedef name tends to
    obscure).

    Furthermore, none of the existing code examples in the current C++
    Standard ever show the result of a sizeof() expression being assigned
    to a variable of size_t; instead, the sizeof value is - in every case
    - assigned to a variable of type int.

    Greg



    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


+ Reply to Thread

Similar Threads

  1. sizeof (size_t) and sizeof (pointer)
    By Application Development in forum c++
    Replies: 19
    Last Post: 11-30-2007, 06:11 PM
  2. The sizeof operator : sizeof(++i)
    By Application Development in forum C
    Replies: 10
    Last Post: 10-19-2007, 04:28 PM
  3. Re: sizeof member
    By Application Development in forum c++
    Replies: 0
    Last Post: 10-05-2007, 11:41 PM
  4. sizeof vector sizeof array different
    By Application Development in forum c++
    Replies: 36
    Last Post: 06-14-2007, 10:32 AM
  5. sizeof(type) and sizeof value
    By Application Development in forum C
    Replies: 4
    Last Post: 09-14-2005, 02:02 AM