sizeof

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cs

    #1

    sizeof

    #include <stdio.h>
    #include <stdlib.h>

    typedef struct{
    unsigned a;
    unsigned *b;
    unsigned k[50];
    }tp;

    tp e, *ee;
    is it true *e.b==*(e.b)?
    is it safe to write sizeof e, sizeof e.a, sizeof e.b, sizeof *e.b,
    sizeof e.k in C code?

    and if sizeof(unsigned )==4 and sizeof(unsigned *)==8 then

    sizeof e.a=4
    sizeof e.b=8
    sizeof *e.b=4
    sizeof e.k=50*4

    is it true ?

    in the case ee=&e;
    is it true *ee->b==*(ee->b)?
    is it safe to write sizeof ee->a, sizeof ee->b, sizeof *ee->b,
    sizeof ee->k in C code?

    sizeof ee->a=4
    sizeof ee->b=8
    sizeof *ee->b=4
    sizeof ee->k=50*4
    Thanks
  • Emmanuel Delahaye

    #2
    Re: sizeof

    cs wrote on 17/07/05 :[color=blue]
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > typedef struct{
    > unsigned a;
    > unsigned *b;
    > unsigned k[50];
    > }tp;
    >
    > tp e, *ee;
    > is it true *e.b==*(e.b)?[/color]

    Dunno. Without further information, the value of the e.b pointer is
    undefined, hence *e.b invokes an undefined behaviour.
    [color=blue]
    > is it safe to write sizeof e, sizeof e.a, sizeof e.b, sizeof *e.b,
    > sizeof e.k in C code?[/color]

    Yes.
    [color=blue]
    > and if sizeof(unsigned )==4 and sizeof(unsigned *)==8 then
    >
    > sizeof e.a=4
    > sizeof e.b=8
    > sizeof *e.b=4
    > sizeof e.k=50*4
    >
    > is it true ?[/color]

    Yes.
    [color=blue]
    > in the case ee=&e;
    > is it true *ee->b==*(ee->b)?[/color]

    b being still undefined, same answer than above.
    [color=blue]
    > is it safe to write sizeof ee->a, sizeof ee->b, sizeof *ee->b,
    > sizeof ee->k in C code?
    >
    > sizeof ee->a=4
    > sizeof ee->b=8
    > sizeof *ee->b=4
    > sizeof ee->k=50*4[/color]

    Yes.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Mal nommer les choses c'est ajouter du malheur au
    monde." -- Albert Camus.


    Comment

    • Lawrence Kirby

      #3
      Re: sizeof

      On Sun, 17 Jul 2005 12:21:13 +0200, Emmanuel Delahaye wrote:
      [color=blue]
      > cs wrote on 17/07/05 :[color=green]
      >> #include <stdio.h>
      >> #include <stdlib.h>
      >>
      >> typedef struct{
      >> unsigned a;
      >> unsigned *b;
      >> unsigned k[50];
      >> }tp;
      >>
      >> tp e, *ee;
      >> is it true *e.b==*(e.b)?[/color]
      >
      > Dunno. Without further information, the value of the e.b pointer is
      > undefined, hence *e.b invokes an undefined behaviour.[/color]

      True, however *e.b and *(e.b) are equivalent expressions. Since . has
      higher precedence than * the e operand is bound to the . operator.

      Lawrence

      Comment

      • cs

        #4
        Re: sizeof

        On Sun, 17 Jul 2005 12:21:13 +0200, "Emmanuel Delahaye"
        <emdel@YOURBRAn oos.fr> wrote:[color=blue]
        >cs wrote:[color=green]
        >> #include <stdio.h>
        >> #include <stdlib.h>
        >>
        >> typedef struct{
        >> unsigned a;
        >> unsigned *b;
        >> unsigned k[50];
        >> }tp;
        >>
        >> tp e, *ee;
        >> is it true *e.b==*(e.b)?[/color]
        >
        >Dunno. Without further information, the value of the e.b pointer is
        >undefined, hence *e.b invokes an undefined behaviour.[/color]

        it seems i understand
        tp e;
        tp *e=&e;
        size_t s0, s1;
        s0=sizeof *e.b; s1=sizeof *ee->b;
        is ok and safe. Is it right?
        Thank you very much.
        [color=blue][color=green]
        >> is it safe to write sizeof e, sizeof e.a, sizeof e.b, sizeof *e.b,
        >> sizeof e.k in C code?[/color]
        >
        >Yes.[/color]

        Comment

        • Barry Schwarz

          #5
          Re: sizeof

          On Mon, 18 Jul 2005 09:47:40 GMT, cs <n@ss.g> wrote:
          [color=blue]
          >On Sun, 17 Jul 2005 12:21:13 +0200, "Emmanuel Delahaye"
          ><emdel@YOURBRA noos.fr> wrote:[color=green]
          >>cs wrote:[color=darkred]
          >>> #include <stdio.h>
          >>> #include <stdlib.h>
          >>>
          >>> typedef struct{
          >>> unsigned a;
          >>> unsigned *b;
          >>> unsigned k[50];
          >>> }tp;
          >>>
          >>> tp e, *ee;
          >>> is it true *e.b==*(e.b)?[/color]
          >>
          >>Dunno. Without further information, the value of the e.b pointer is
          >>undefined, hence *e.b invokes an undefined behaviour.[/color]
          >
          >it seems i understand
          >tp e;
          >tp *e=&e;[/color]

          You must mean *ee here.
          [color=blue]
          >size_t s0, s1;
          >s0=sizeof *e.b; s1=sizeof *ee->b;
          >is ok and safe. Is it right?[/color]

          e.b and ee->b refer to the same variable. Consequently, the two
          expressions are equivalent. Furthermore, since sizeof does not
          evaluate its operand, it is safe to use either expression as the
          operand. This would even be true if ee were not initialized.

          The point ED was making was that since you did not initialize e.b, any
          attempt to dereference it, as with *e.b, invokes undefined behavior.
          This is true with or without the parenthesis.



          <<Remove the del for email>>

          Comment

          Working...