C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sizeof of a class member?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Steve
Guest





PostPosted: Tue Dec 26, 2006 5:43 am    Post subject: sizeof of a class member? Reply with quote



Why does not the following code compile?

struct A
{
int a;
const static int size_a = sizeof(a);
};


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





PostPosted: Wed Dec 27, 2006 2:26 am    Post subject: Re: sizeof of a class member? Reply with quote



This is because the size of the data member can be deduced only when it
gets defined. i.e., it can be accessed only through an object of A.

Thanks & Regards
Venkatesh
Steve wrote:
Quote:
Why does not the following code compile?

struct A
{
int a;
const static int size_a = sizeof(a);
};

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
perrog@gmail.com
Guest





PostPosted: Wed Dec 27, 2006 2:31 am    Post subject: Re: sizeof of a class member? Reply with quote



Gianni Mariani skrev:

Quote:
Steve wrote:
Why does not the following code compile?

struct A
{
int a;
const static int size_a = sizeof(a);
};
[ ... ]
It likes this:

struct A
{
int a;
static const int size_a = sizeof(A().a);
};

If A has no default destructor, you're outa luck.

Then NULL pointers becomes your friend.

struct A
{
int a;
const static int size_a;
};

#define sizeof_member(TYPE, MEMBER) \
sizeof((static_cast<TYPE *> (0)->MEMBER))

const int A::size_a = sizeof_member(A, a);


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





PostPosted: Wed Dec 27, 2006 2:33 am    Post subject: Re: sizeof of a class member? Reply with quote

Thanks Ivan and all other replies.

How about the following code?
struct A
{
int a;
int array_a[sizeof(a)]; // not okay
int func() { return sizeof(a);} //okay.
};

I guess I was a little unclear on the scope of "a" and when it can be
used by sizeof. It looks like that we can't use sizeof of a non-static
class member in the class' declaration scope, but okay within its
definition scope.

In other words, the class has to be complete if we wants to get the
complie time information of its non-static data member? Not sure why
this limitation is needed.

Steve



Ivan Novick wrote:
Quote:
Steve wrote:
Why does not the following code compile?

struct A
{
int a;
const static int size_a = sizeof(a);
};

The short answer: because 'a' is not static.

The long answer: according to the standard section 9.4/4: "The
definition of a static member shall not use directly the names of the
nonstatic members of its class or of a base class of its class
(including as operands of the sizeof operator)."

----
Ivan
http://www.0x4849.net

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





PostPosted: Sat Jan 06, 2007 3:20 am    Post subject: Re: sizeof of a class member? Reply with quote

On 31 Dec 2006 13:56:02 -0500, James Kanze wrote:

Quote:
(And as another poster has
pointed out, you can freely dereference a null pointer without
risking undefined behavior.)

Are you sure? Core issue 232 is still in drafting state, and core 315
(the closest closed issue Smile) doesn't apply.

___
\|||/ Gennaro Prota - For hire
(o o) https://sourceforge.net/projects/breeze/
--ooO-(_)-Ooo----- (to mail: name _ surname / yaho ! com)

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





PostPosted: Sun Jan 07, 2007 4:44 am    Post subject: Re: sizeof of a class member? Reply with quote

Gennaro Prota wrote:
Quote:
On 31 Dec 2006 13:56:02 -0500, James Kanze wrote:

(And as another poster has
pointed out, you can freely dereference a null pointer without
risking undefined behavior.)

Are you sure? Core issue 232 is still in drafting state, and core 315
(the closest closed issue Smile) doesn't apply.

You've cut some important context. In a sizeof expression, you
can freely dereference a null pointer without risking undefined
behavior, because the argument to a sizeof expression isn't
evaluated. Dereferencing a null pointer is only undefined
behavior is the expression is actually evaluated at run-time.

--
James Kanze (Gabi Software) email: james.kanze (AT) gmail (DOT) com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


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





PostPosted: Sun Jan 07, 2007 4:44 am    Post subject: Re: sizeof of a class member? Reply with quote

Ivan Novick wrote:
Quote:
James Kanze wrote:
Steve wrote:
How about the following code?
struct A
{
int a;
int array_a[sizeof(a)]; // not okay
int func() { return sizeof(a);} //okay.
};
It's not a question of completeness. The problem is that sizeof
requires a legal expression or the name of a type. Outside of a
member function, "a" is neither. Within a non-static member
function, "a" is a legal expression equivalent to "this->a".
Logically, of course, you wouldn't expect to be able to use "a"
outside of a non-static member function, except as an operand to
sizeof (where the expression isn't evaluated). Since there are
no special rules for sizeof, except to allow type names, it
doesn't work, even though it conceivably could be made to work.

Hey wait.. on gcc 3.2.3 i get:
member `Foo::msg' is non-static but referenced as a static member

For this code:
#include <cstdio

struct Foo
{
char msg[50];
char data[50];
};

int main(int argc, char** argv)
{
printf("sizeof = %s\n", sizeof(Foo::msg));
return 0;
}

I don't see anyting in section 5.3.3 that says you can't take size of
class member?

As I said before, the argument of sizeof must be either the name
of a type, or a legal expression. In main(), Foo::msg is
neither. (It would be a legal expression if msg were a static
member.)

What g++ is complaining about is the fact that Foo::msg is not a
legal expression. It will complain if you use it other than in
a sizeof expression as well. For the sizeof expression to be
legal, there would have to be special text to allow it.

--
James Kanze (Gabi Software) email: james.kanze (AT) gmail (DOT) com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Clark S. Cox III
Guest





PostPosted: Mon Jan 08, 2007 10:10 am    Post subject: Re: sizeof of a class member? Reply with quote

Gennaro Prota wrote:
Quote:
On 6 Jan 2007 17:44:06 -0500, James Kanze wrote:

You've cut some important context. In a sizeof expression, you
can freely dereference a null pointer without risking undefined
behavior, because the argument to a sizeof expression isn't
evaluated. Dereferencing a null pointer is only undefined
behavior is the expression is actually evaluated at run-time.

Hmm, perhaps... I'm not able to back that up from the standard,
though.


You didn't look very hard then :)

The first two sentences describing the sizeof operator:

5.3.3 Sizeof
1 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 ...


--
Clark S. Cox III
clarkcox3 (AT) gmail (DOT) com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.