Topic: sizeof
Author: d96-mst@nada.kth.se (Mikael Steldal)
Date: 1997/04/13 Raw View
In article <orhghhjlqu.fsf@sunsite.dcc.unicamp.br>,
Alexandre Oliva <oliva@dcc.unicamp.br> wrote:
>> Some suggested sizeof(foo::b) which seems to work in my compiler. Is that
>> valid according to the draft standard?
>
>Nope.
Then I propose that the standard draft is changed so that this IS allowed.
Some compilers do support it (it seems like gcc does) and I can't find out
anyway it could cause trouble. I think it is useful to be able to get the
size of a struct/class member without any fuss.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: WSEYMOUR@email.usps.gov
Date: 1997/04/14 Raw View
Mikael Steldal wrote:
>
> In article <orhghhjlqu.fsf@sunsite.dcc.unicamp.br>,
> Alexandre Oliva <oliva@dcc.unicamp.br> wrote:
>
> >> Some suggested sizeof(foo::b) which seems to work
> >> in my compiler. Is that valid according to the
> >> draft standard?
> >
> > Nope.
>
> Then I propose that the standard draft is changed
> so that this IS allowed. Some compilers do support it
> (it seems like gcc does) and I can't find out anyway
> it could cause trouble. I think it is useful to be able
> to get the size of a struct/class member without any fuss.
>
I proposed a binary :: operator for C9X (as part of the
Member Functions proposal which died) that would have
just that functionality. I think it's simple, direct
and readable; but I also know that it comes too late
for either C or C++.
Actually, there's a pretty ugly workaround:
sizeof ((type*)0)->member
which looks dangerously like the old offsetof hack
until you remember that sizeof's operand isn't
evaluated, so you're not really dereferencing
a null pointer.
--Bill
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: d96-mst@nada.kth.se (Mikael St ldal)
Date: 1997/03/31 Raw View
Is this code valid? Is it OK to apply the sizeof operator to one member of
a struct/class?
#include <cstddef>
struct foo
{
int a;
char b[17];
};
size_t bar(void)
{
return sizeof(foo.b);
}
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/04/01 Raw View
d96-mst@nada.kth.se (Mikael Steldal) writes:
>Is this code valid?
No.
>Is it OK to apply the sizeof operator to one member of
>a struct/class?
>
>#include <cstddef>
>
>struct foo
>{
> int a;
> char b[17];
>};
>
>size_t bar(void)
>{
> return sizeof(foo.b);
>}
First, you need to add `using std;', or change `size_t' to `std::size_t',
or change `<cstddef>' to `<stddef.h>'.
Second, `foo.b' is not valid as either a type or an expression.
The argument to sizeof must be one of the two. Instead, you can use
`sizeof foo().b'. Here `foo().b' is an expression that creates an
object of type `foo', and then extracts its `b' field; since you're
only asking for its size, this expression will not be evaluated.
In the general case, if `foo' does not have a default constructor, then
you may need to use `sizeof ((foo *)0)->b' or something like that.
Note that although this code looks suspiciously like it is dereferencing
a null pointer, that does not happen, because the expression is not
evaluated.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/04/01 Raw View
Mikael St=E5ldal writes:
> Is this code valid? Is it OK to apply the sizeof operator to one member=
of
> a struct/class?
Nope. The operand of sizeof must be either an expression, that is not
evaluated, or a type-id.
Since the expression is not evaluated, you could obtain the desired
effect with:
sizeof(((foo*)0)->b);
which could obviously be turned into a macro, or use the following
not-so-easy-to-use template:
template <class C, class T>
size_t sizeofmember(T C::*) {
return sizeof(T);
}
> return sizeof(foo.b);
// would become:
return sizeofmember(&foo::b);
--=20
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: d96-mst@nada.kth.se (Mikael Steldal)
Date: 1997/04/04 Raw View
In article <5hr3ov$4qh@mulga.cs.mu.OZ.AU>,
fjh@mundook.cs.mu.OZ.AU (Fergus Henderson) wrote:
>> return sizeof(foo.b);
>
>Second, `foo.b' is not valid as either a type or an expression.
>The argument to sizeof must be one of the two. Instead, you can use
>`sizeof foo().b'.
Some suggested sizeof(foo::b) which seems to work in my compiler. Is that
valid according to the draft standard?
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/04/09 Raw View
Mikael Steldal writes:
> In article <5hr3ov$4qh@mulga.cs.mu.OZ.AU>,
> fjh@mundook.cs.mu.OZ.AU (Fergus Henderson) wrote:
>>> return sizeof(foo.b);
>>
>> Second, `foo.b' is not valid as either a type or an expression.
>> The argument to sizeof must be one of the two. Instead, you can use
>> `sizeof foo().b'.
> Some suggested sizeof(foo::b) which seems to work in my compiler. Is that
> valid according to the draft standard?
Nope.
The argument to sizeof must be either a type-id or a valid expression,
and foo::b is a valid expression only in some contexts.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]