sizeof an element of a structure 
Author Message
 sizeof an element of a structure

If I have a structure:

typedef struct
{   type1_t     k1;
    type2_t     k2;
    type3_t     k3;

Quote:
} mystruct_t;

and I want to get the size of k3, is:

        sizeof(type3_t)

the best I can do? Is there a syntax something like:

        sizeof(mystruct_t.k3)

which will give me the size of k3 even if it's type changes?

--
--

--



Sun, 21 Nov 2004 08:48:35 GMT  
 sizeof an element of a structure

Quote:
> If I have a structure:

> typedef struct
> {   type1_t     k1;
>     type2_t     k2;
>     type3_t     k3;
> } mystruct_t;

> and I want to get the size of k3, is:

> sizeof(type3_t)

> the best I can do? Is there a syntax something like:

> sizeof(mystruct_t.k3)

> which will give me the size of k3 even if it's type changes?

Yes indeed.  This is very close to the syntax.  I'm not sure if
the syntax you've suggested will work, but what will work is
taking the sizeof a member of an object of that type.  Hence:

mystruct_t thing;
size_t things_size = sizeof(thing.k3);

OR

size_t f(mystruct_t *thing) {
    return sizeof(thing->k3);

Quote:
}

These are portable.  I've seen other variants that don't use an
instance but look like dreadful hacks.  My write-only memory
brings the following to mind:

sizeof((mystruct_t*)(0)->k3)

Of course, I could be wrong.  The theory is that this construct
pretends to make a reference to an object at address 0 of type
mystruct_t and to then point to the k3 element for the sizeof
operation.  I *will not* guarantee that this works.

Geoff

--
Geoff Field,    Professional geek, amateur stage-levelling gauge.


au
The suespammers.org mail server is located in California; do not send
unsolicited bulk e-mail or unsolicited commercial e-mail to my
suespammers.org address
--



Sun, 21 Nov 2004 22:57:30 GMT  
 sizeof an element of a structure

comp.lang.c.moderated:

Quote:
> If I have a structure:

> typedef struct
> {   type1_t     k1;
>     type2_t     k2;
>     type3_t     k3;
> } mystruct_t;

> and I want to get the size of k3, is:

>    sizeof(type3_t)

> the best I can do? Is there a syntax something like:

>    sizeof(mystruct_t.k3)

> which will give me the size of k3 even if it's type changes?

The sizeof operator requires either the name of a type (built-in or
defined) which must be enclosed in parentheses, or a reference to an
object from which it can determine the type, for which parentheses are
optional.

The name of a structure element is neither a type nor a reference to
an object.

But you can take advantage of the fact that sizeof is a compile time
operator and (except for the new VLAs in C99) does not evaluate its
operand at all, just parses the expression at compile time to
determine the type, then converts it to a constant expression of type
size_t.

So what you can do is this:

sizeof ((mystruct *)0)->type3_t;

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
--



Sun, 21 Nov 2004 22:57:35 GMT  
 sizeof an element of a structure

Quote:
>If I have a structure:

>typedef struct
>{   type1_t     k1;
>    type2_t     k2;
>    type3_t     k3;
>} mystruct_t;

>and I want to get the size of k3, is:

>    sizeof(type3_t)

>the best I can do? Is there a syntax something like:

>    sizeof(mystruct_t.k3)

>which will give me the size of k3 even if it's type changes?

You can use sizeof (mystruct_t*)0->k3.

<<Remove the del for email>>
--



Sun, 21 Nov 2004 22:57:40 GMT  
 sizeof an element of a structure

Quote:
> If I have a structure:

> typedef struct
> {   type1_t     k1;
>     type2_t     k2;
>     type3_t     k3;
> } mystruct_t;

> and I want to get the size of k3, is:

> sizeof(type3_t)

> the best I can do? Is there a syntax something like:

> sizeof(mystruct_t.k3)

> which will give me the size of k3 even if it's type changes?

You can do this:

sizeof(((mystruct_t *)0)->k3)

Remember that an expression operand to sizeof is not evaluated; just the
type is determined.

--
Eric Amick
Columbia, MD
--



Sun, 21 Nov 2004 22:57:43 GMT  
 sizeof an element of a structure

Quote:

> If I have a structure:

> typedef struct
> {   type1_t     k1;
>     type2_t     k2;
>     type3_t     k3;
> } mystruct_t;

> and I want to get the size of k3, is:

>         sizeof(type3_t)

> the best I can do? Is there a syntax something like:

>         sizeof(mystruct_t.k3)

> which will give me the size of k3 even if it's type changes?

Yes.  You can also use:

      sizeof mystruct_t.k3

since sizeof is an operator.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!
--



Sun, 21 Nov 2004 22:57:45 GMT  
 sizeof an element of a structure

Quote:
> If I have a structure:

> typedef struct
> {   type1_t     k1;
>     type2_t     k2;
>     type3_t     k3;
> } mystruct_t;

> and I want to get the size of k3, is:

>    sizeof(type3_t)

> the best I can do? Is there a syntax something like:

>    sizeof(mystruct_t.k3)

For using the dot operator, you need an instance of the struct,
such as:

mystruct_t S;

  sizeof(S.k3);

Quote:
> which will give me the size of k3 even if it's type changes?

Seeing you're using typedef, I think your doing this in C.
In C++, you have the scope operator, so you could do:

sizeof(mystruct_t::k3);

Regards,
David.

Reply: replace 'nospam' with my lastname
--
comp.lang.c     FAQ: http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++   FAQ: http://www.parashift.com/c++-faq-lite/
Online book reviews: http://www.accu.org/bookreviews/public
--



Sun, 21 Nov 2004 22:57:52 GMT  
 sizeof an element of a structure

Quote:
>If I have a structure:

>typedef struct
>{   type1_t     k1;
>    type2_t     k2;
>    type3_t     k3;
>} mystruct_t;

>and I want to get the size of k3, is:

>    sizeof(type3_t)

>the best I can do? Is there a syntax something like:

>    sizeof(mystruct_t.k3)

>which will give me the size of k3 even if it's type changes?

The sizeof operator requires either a type name or an expression.
Your mystruct_t.k3 construction is neither.

If, however, you have an object of type mystruct_t, you can use that,
e.g.

   mystruct_t x;
   size_t sz;

   sz = sizeof(x.k3);

HTH,

                               -=Dave
--
Change is inevitable, progress is not.
--



Sun, 21 Nov 2004 22:57:54 GMT  
 sizeof an element of a structure

Quote:

> {   type1_t     k1;
>     type2_t     k2;
>     type3_t     k3;
> } mystruct_t;
> and I want to get the size of k3, is:
>    sizeof(type3_t)
> the best I can do? Is there a syntax something like:

>    sizeof(mystruct_t.k3)

No.  But there is

        mystruct_t variable;
        sizeof(variable.k3)

This lends itself to the following somewhat horrible trick:

        sizeof(((mystruct_t *)0)->k3);

As to whether or not this is legal C or causes undefined behaviour, I
must admit I'm a bit lost.  But I suspect it's undefined behaviour.
--

Even if all the snow were burnt, ashes would remain.
--



Sun, 21 Nov 2004 22:58:00 GMT  
 sizeof an element of a structure

Quote:

> If I have a structure:

> typedef struct
> {   type1_t     k1;
>     type2_t     k2;
>     type3_t     k3;
> } mystruct_t;

> and I want to get the size of k3, is:

>    sizeof(type3_t)

> the best I can do? Is there a syntax something like:

>    sizeof(mystruct_t.k3)

> which will give me the size of k3 even if it's type changes?

sizeof(((struct mystruct_t*)0)->k3);
--



Sun, 21 Nov 2004 22:58:02 GMT  
 sizeof an element of a structure

Quote:

>>If I have a structure:

>>typedef struct
>>{   type1_t     k1;
>>    type2_t     k2;
>>    type3_t     k3;
>>} mystruct_t;

>>and I want to get the size of k3, is:

>>        sizeof(type3_t)

>>the best I can do? Is there a syntax something like:

>>        sizeof(mystruct_t.k3)

>>which will give me the size of k3 even if it's type changes?

>You can use sizeof (mystruct_t*)0->k3.

No, you can't, because this is parsed as sizeof(mystruct_t*) followed
by the nonsensical (in context) 0->k3.  At least on extra pair of
parentheses is needed.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

--



Mon, 22 Nov 2004 03:10:29 GMT  
 sizeof an element of a structure

Quote:

>> If I have a structure:

>> typedef struct
>> {   type1_t     k1;
>>     type2_t     k2;
>>     type3_t     k3;
>> } mystruct_t;

>> and I want to get the size of k3, is:

>>         sizeof(type3_t)

>> the best I can do? Is there a syntax something like:

>>         sizeof(mystruct_t.k3)

>> which will give me the size of k3 even if it's type changes?

>Yes.  You can also use:

>      sizeof mystruct_t.k3

>since sizeof is an operator.

Nope: the . operator can't have a type as its LHS operand.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

--



Mon, 22 Nov 2004 03:10:31 GMT  
 sizeof an element of a structure

Quote:


> > If I have a structure:

> > typedef struct
> > {   type1_t     k1;
> >     type2_t     k2;
> >     type3_t     k3;
> > } mystruct_t;

> > and I want to get the size of k3, is:

> >         sizeof(type3_t)

> > the best I can do? Is there a syntax something like:

> >         sizeof(mystruct_t.k3)

> > which will give me the size of k3 even if it's type changes?

> Yes.  You can also use:

>       sizeof mystruct_t.k3

> since sizeof is an operator.

Oops, goofed, missed the fact that mystruct_t is a typedef.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!
--



Tue, 23 Nov 2004 23:51:20 GMT  
 sizeof an element of a structure

...

Quote:

> This lends itself to the following somewhat horrible trick:

>    sizeof(((mystruct_t *)0)->k3);

> As to whether or not this is legal C or causes undefined behaviour, I
> must admit I'm a bit lost.  But I suspect it's undefined behaviour.

I don't think so (I mean, I think it is defined behaviour).
The standard (C99) says (section 6.5.3.4, "The sizeof operator"):

2 The sizeof operator yields the size (in bytes) of its operand, which
  may be an expression or the parenthesized name of a type. The size is
  determined from the type of the operand. The result is an integer. If
  the type of the operand is a variable length array type, the operand
  is evaluated; otherwise, the operand is not evaluated and the result
                                          ^^^^^^^^^^^^^
  is an integer constant.

and a couple of lines after that an example is presented that uses an
expression in which the address of the data (dp) is not known at all:

5 EXAMPLE 1 A principal use of the sizeof operator is in communication
  with routines such as storage allocators and I/O systems. A
  storage-allocation function might accept a size (in bytes) of an object
  to allocate and return a pointer to void. For example:

    extern void *alloc(size_t);
    double *dp = alloc(sizeof *dp);

--
All theoretical chemistry is really physics;
and all theoretical chemists know it.
                -- Richard P. Feynman

Rob Windgassen
--



Tue, 23 Nov 2004 23:51:30 GMT  
 sizeof an element of a structure
On 05 Jun 2002 14:58:00 GMT, Hans-Bernhard Broeker

Quote:


>> {   type1_t     k1;
>>     type2_t     k2;
>>     type3_t     k3;
>> } mystruct_t;

>> and I want to get the size of k3, is:

>>        sizeof(type3_t)

>> the best I can do? Is there a syntax something like:

>>        sizeof(mystruct_t.k3)

>No.  But there is

>    mystruct_t variable;
>    sizeof(variable.k3)

>This lends itself to the following somewhat horrible trick:

>    sizeof(((mystruct_t *)0)->k3);

>As to whether or not this is legal C or causes undefined behaviour, I
>must admit I'm a bit lost.  But I suspect it's undefined behaviour.

AFAIK it's defined behaviour, as it's similar to the technique
normally used by the standard offsetof(type,member) macro (in
stddef.h) to generate the offset of member in type, e.g. (with
some parentheses omitted for readability):

#define offsetof(type,member) (size_t) &((type *)0)->member

--

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada


    fake address                use address above to reply


                                                spam traps
--



Fri, 26 Nov 2004 09:04:19 GMT  
 
 [ 24 post ]  Go to page: [1] [2]

 Relevant Pages 

1. help on sizeof of structure element

2. sizeof (struct) ! = sizeof struct elements

3. making pointers to structure elements using array elements

4. Number of elements akin to sizeof()

5. sizeof a structure member

6. Find the sizeof a structure

7. sizeof a structure

8. sizeof structure members

9. sizeof operator & structures

10. Sizeof structure

11. C++ (Was Re: typeof(), like sizeof() (Was: Question on structures))

12. Arrays of Unknown Length in Structures (and sizeof)

 

 
Powered by phpBB® Forum Software