Struct assignment

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

    #1

    Struct assignment

    If I have the ff struct:

    struct A
    {
    unsigned int i;
    char s[LONG_ENOUGH];
    } a, b;


    And use them in code like this:

    a.i = 42 ;
    strcpy(a.s,"tes t");

    b.i = 100 ;

    b = a ;

    at this point, a (bitwise?) copy of a is made to b. Question is:

    1). is b.s now ptr to a.s ? (I think so)
    If so, what happens if for instance variable 'a' goes out of scope (?)

    2). Does the compiler generate an implicit "memcpy" or "memmove" behind
    the scenes when it sees an assignment like this (to avoid dangling ptrs)?
  • Malcolm McLean

    #2
    Re: Struct assignment


    "Grey Alien" <grey@andromeda .comwrote in message
    news:erydna87m6 gBFRvbRVnyjwA@b t.com...
    If I have the ff struct:
    >
    struct A
    {
    unsigned int i;
    char s[LONG_ENOUGH];
    } a, b;
    >
    >
    And use them in code like this:
    >
    a.i = 42 ;
    strcpy(a.s,"tes t");
    >
    b.i = 100 ;
    >
    b = a ;
    >
    at this point, a (bitwise?) copy of a is made to b. Question is:
    >
    1). is b.s now ptr to a.s ? (I think so)
    If so, what happens if for instance variable 'a' goes out of scope (?)
    >
    2). Does the compiler generate an implicit "memcpy" or "memmove" behind
    the scenes when it sees an assignment like this (to avoid dangling ptrs)?
    >
    The answer is 2. A structure assignment will cause a call to memcpy to be
    made, or equivalent code emitted if the structure is small enough to make
    this wasteful and the compiler is clever.
    However if structures contain pointers then the values of the pointers are
    overwritten. So you have to be extremely careful not to orphan memory or
    create peculiar bugs with aliases.

    --
    Free games and programming goodies.




    Comment

    • Thomas Lumley

      #3
      Re: Struct assignment

      On Jun 30, 9:43 am, Grey Alien <g...@andromeda .comwrote:
      If I have the ff struct:
      >
      struct A
      {
      unsigned int i;
      char s[LONG_ENOUGH];
      >
      } a, b;
      >
      And use them in code like this:
      >
      a.i = 42 ;
      strcpy(a.s,"tes t");
      >
      b.i = 100 ;
      >
      b = a ;
      >
      at this point, a (bitwise?) copy of a is made to b.
      Question is:
      >
      1). is b.s now ptr to a.s ? (I think so)
      I think you are confusing arrays and pointers. Since a.s is an array,
      a.s[0] to a.s[LONG_ENOUGH-1] are actually stored in the structure and
      are copied by the assignment.

      If a.s were just a pointer to memory allocated elsewhere the
      assignment would just copy the pointer.
      2). Does the compiler generate an implicit "memcpy"
      or "memmove" behind the scenes when it sees an
      assignment like this (to avoid dangling ptrs)?
      There is an explicit copy since a.s is an array. If a.s
      were a pointer there would not be an implicit copy, and
      Bad Things such as dangling pointers could result.

      -thomas

      Comment

      • Flash Gordon

        #4
        Re: Struct assignment

        Grey Alien wrote, On 30/06/07 17:43:
        If I have the ff struct:
        >
        struct A
        {
        unsigned int i;
        char s[LONG_ENOUGH];
        } a, b;
        >
        >
        And use them in code like this:
        >
        a.i = 42 ;
        strcpy(a.s,"tes t");
        >
        b.i = 100 ;
        >
        b = a ;
        >
        at this point, a (bitwise?) copy of a is made to b.
        It is not a bitwise copy, it is a copy of all the elements in the struct.
        Question is:
        >
        1). is b.s now ptr to a.s ? (I think so)
        Since b.s was not defined as a pointer, what makes you think an
        assignment could magically transform it from being an array in to being
        a pointer? You need to read section 6 of the comp.lang.c FAQ at
        http://c-faq.com/ specifically the questions dealing with whether
        pointers and arrays are the same thing.
        If so, what happens if for instance variable 'a' goes out of scope (?)
        >
        2). Does the compiler generate an implicit "memcpy" or "memmove" behind
        the scenes when it sees an assignment like this (to avoid dangling ptrs)?
        It is very rare for C to do things behind your back. Had there been
        pointers in your struct (which there were not) then after the assignment
        they would point to the same place as they point in the original struct,
        and when that place is no longer valid (an automatic that goes out of
        scope, for example) the pointers are no longer valid.
        --
        Flash Gordon

        Comment

        • Serve Lau

          #5
          Re: Struct assignment


          "Flash Gordon" <spam@flash-gordon.me.ukwro te in message
          news:hhfil4xnvp .ln2@news.flash-gordon.me.uk...
          Grey Alien wrote, On 30/06/07 17:43:
          >If I have the ff struct:
          >>
          >struct A
          >{
          > unsigned int i;
          > char s[LONG_ENOUGH];
          >} a, b;
          >>
          >>
          >And use them in code like this:
          >>
          >a.i = 42 ;
          >strcpy(a.s,"te st");
          >>
          >b.i = 100 ;
          >>
          >b = a ;
          >>
          >at this point, a (bitwise?) copy of a is made to b.
          >
          It is not a bitwise copy, it is a copy of all the elements in the struct.
          >
          Question is:
          >>
          >1). is b.s now ptr to a.s ? (I think so)
          >
          Since b.s was not defined as a pointer, what makes you think an assignment
          could magically transform it from being an array in to being a pointer?
          You need to read section 6 of the comp.lang.c FAQ at http://c-faq.com/
          specifically the questions dealing with whether pointers and arrays are
          the same thing.
          Its easy to see where the confusion comes from. There are situations where
          pointers degenerate into pointers , the OP probably had that in mind



          Comment

          • Richard Heathfield

            #6
            Re: Struct assignment

            Serve Lau said:

            <snip>
            Its easy to see where the confusion comes from. There are situations
            where pointers degenerate into pointers ,
            Pointers always degenerate into pointers.

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • Flash Gordon

              #7
              Re: Struct assignment

              Serve Lau wrote, On 30/06/07 19:53:
              "Flash Gordon" <spam@flash-gordon.me.ukwro te in message
              news:hhfil4xnvp .ln2@news.flash-gordon.me.uk...
              >Grey Alien wrote, On 30/06/07 17:43:
              <snip>
              >>1). is b.s now ptr to a.s ? (I think so)
              >Since b.s was not defined as a pointer, what makes you think an assignment
              >could magically transform it from being an array in to being a pointer?
              >You need to read section 6 of the comp.lang.c FAQ at http://c-faq.com/
              >specifically the questions dealing with whether pointers and arrays are
              >the same thing.
              >
              Its easy to see where the confusion comes from. There are situations where
              pointers degenerate into pointers , the OP probably had that in mind
              In my opinion it is only easy to confuse arrays and pointers if it is
              badly taught. If arrays and pointers are taught as fundamentally
              different concepts and *then* the way array names degenerate to pointers
              to the first element is explained there will not be anything like the
              problems. In one-to-one sessions I've been able to explain the basics of
              arrays pointers to non-computer people in minutes (I needed to so I
              could explain approximately what was the cause of a problem), although I
              did not go on to how things are done in C.
              --
              Flash Gordon

              Comment

              • Keith Thompson

                #8
                Re: Struct assignment

                Flash Gordon <spam@flash-gordon.me.ukwri tes:
                Grey Alien wrote, On 30/06/07 17:43:
                >If I have the ff struct:
                >struct A
                >{
                > unsigned int i;
                > char s[LONG_ENOUGH];
                >} a, b;
                >And use them in code like this:
                >a.i = 42 ;
                >strcpy(a.s,"te st");
                >b.i = 100 ;
                >b = a ;
                >at this point, a (bitwise?) copy of a is made to b.
                >
                It is not a bitwise copy, it is a copy of all the elements in the struct.
                Yes -- and that can be, and commonly is, implemented as a bitwise copy
                of the struct.

                Suppose there's a gap between the members "i" and "s". After the
                assignment "b = a;", the gaps in "a" and "b" may or may not have the
                same contents. The assignment can be done either as a bitwise copy or
                by copying the members one-by-one, leaving any gaps alone.

                99% of the type, this doesn't matter because you're never going to
                look at what's in the gaps anyway.

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                "We must do something. This is something. Therefore, we must do this."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • Flash Gordon

                  #9
                  Re: Struct assignment

                  Richard Heathfield wrote, On 30/06/07 20:56:
                  Serve Lau said:
                  >
                  <snip>
                  >
                  >Its easy to see where the confusion comes from. There are situations
                  >where pointers degenerate into pointers ,
                  >
                  Pointers always degenerate into pointers.
                  No they don't, they stay as pointers ;-)

                  Knowing what Serve Lau must have intended I read it as "where arrays
                  degenerate...".
                  --
                  Flash Gordon

                  Comment

                  • Richard Heathfield

                    #10
                    Re: Struct assignment

                    Flash Gordon said:
                    Richard Heathfield wrote, On 30/06/07 20:56:
                    >Serve Lau said:
                    >>
                    ><snip>
                    >>
                    >>Its easy to see where the confusion comes from. There are situations
                    >>where pointers degenerate into pointers ,
                    >>
                    >Pointers always degenerate into pointers.
                    >
                    No they don't, they stay as pointers ;-)
                    >
                    Knowing what Serve Lau must have intended I read it as "where arrays
                    degenerate...".
                    I know. Actually, arrays never actually do that. We often say they do,
                    but we're just being lazy. What we really mean is that the name of an
                    array, when used in an expression, is often (indeed, *usually*) treated
                    as if it were a pointer to the array's first element. The array itself
                    never "degenerate s" (or "decays", as it is usually put) at all.

                    --
                    Richard Heathfield <http://www.cpax.org.uk >
                    Email: -www. +rjh@
                    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                    "Usenet is a strange place" - dmr 29 July 1999

                    Comment

                    • Flash Gordon

                      #11
                      Re: Struct assignment

                      Keith Thompson wrote, On 30/06/07 21:30:
                      Flash Gordon <spam@flash-gordon.me.ukwri tes:
                      >Grey Alien wrote, On 30/06/07 17:43:
                      >>If I have the ff struct:
                      >>struct A
                      >>{
                      >> unsigned int i;
                      >> char s[LONG_ENOUGH];
                      >>} a, b;
                      >>And use them in code like this:
                      >>a.i = 42 ;
                      >>strcpy(a.s,"t est");
                      >>b.i = 100 ;
                      >>b = a ;
                      >>at this point, a (bitwise?) copy of a is made to b.
                      >It is not a bitwise copy, it is a copy of all the elements in the struct.
                      >
                      Yes -- and that can be, and commonly is, implemented as a bitwise copy
                      of the struct.
                      >
                      Suppose there's a gap between the members "i" and "s". After the
                      assignment "b = a;", the gaps in "a" and "b" may or may not have the
                      same contents. The assignment can be done either as a bitwise copy or
                      by copying the members one-by-one, leaving any gaps alone.
                      >
                      99% of the type, this doesn't matter because you're never going to
                      look at what's in the gaps anyway.
                      Where there are multiple representations for the same value the
                      representation could change. It is important for the OP to know this
                      IMHO so that s/he does not assume that memcmp can be used safely.
                      --
                      Flash Gordon

                      Comment

                      • Army1987

                        #12
                        Re: Struct assignment


                        "Grey Alien" <grey@andromeda .comha scritto nel messaggio news:erydna87m6 gBFRvbRVnyjwA@b t.com...
                        If I have the ff struct:
                        >
                        struct A
                        {
                        unsigned int i;
                        char s[LONG_ENOUGH];
                        } a, b;
                        >
                        >
                        And use them in code like this:
                        >
                        a.i = 42 ;
                        strcpy(a.s,"tes t");
                        >
                        b.i = 100 ;
                        >
                        b = a ;
                        >
                        at this point, a (bitwise?) copy of a is made to b. Question is:
                        >
                        1). is b.s now ptr to a.s ? (I think so)
                        No, since it is an array, not a pointer.
                        If so, what happens if for instance variable 'a' goes out of scope (?)
                        >
                        2). Does the compiler generate an implicit "memcpy" or "memmove" behind the scenes when it sees an assignment like this?
                        It could, but it needn't. For example, padding bits needn't be
                        copied.


                        Comment

                        • Eric Sosman

                          #13
                          Re: Struct assignment

                          Malcolm McLean wrote On 06/30/07 13:05,:
                          "Grey Alien" <grey@andromeda .comwrote in message
                          news:erydna87m6 gBFRvbRVnyjwA@b t.com...
                          >
                          >>If I have the ff struct:
                          >>
                          >>struct A
                          >>{
                          > unsigned int i;
                          > char s[LONG_ENOUGH];
                          >>} a, b;
                          >>
                          >>
                          >>And use them in code like this:
                          >>
                          >>a.i = 42 ;
                          >>strcpy(a.s,"t est");
                          >>
                          >>b.i = 100 ;
                          >>
                          >>b = a ;
                          >>
                          >>at this point, a (bitwise?) copy of a is made to b. Question is:
                          >>
                          >>1). is b.s now ptr to a.s ? (I think so)
                          >>If so, what happens if for instance variable 'a' goes out of scope (?)
                          >>
                          >>2). Does the compiler generate an implicit "memcpy" or "memmove" behind
                          >>the scenes when it sees an assignment like this (to avoid dangling ptrs)?
                          >>
                          >
                          The answer is 2. A structure assignment will cause a call to memcpy to be
                          made, or equivalent code emitted if the structure is small enough to make
                          this wasteful and the compiler is clever. [...]
                          The compiler *may* do this, but it is not required
                          to copy padding bytes and padding bits. Assignment only
                          copies the value; padding bytes and bits are not part of
                          the value of a struct, and might not be copied -- as they
                          would be if memcpy() or an equivalent were used.

                          --
                          Eric.Sosman@sun .com

                          Comment

                          • Old Wolf

                            #14
                            Re: Struct assignment

                            On Jul 3, 2:59 am, Eric Sosman <Eric.Sos...@su n.comwrote:
                            The compiler *may* do this, but it is not required
                            to copy padding bytes and padding bits. Assignment only
                            copies the value; padding bytes and bits are not part of
                            the value of a struct, and might not be copied -- as they
                            would be if memcpy() or an equivalent were used.
                            Something that just occurred to me. Say we
                            have:
                            struct S
                            {
                            char s[10];
                            };

                            struct S s = { "hello" };
                            struct S t;
                            t = s;

                            Initializing an automatic array from a string
                            literal is only guaranteed to initialize the
                            characters of the string and the null terminator
                            - it could leave subsequent array entries (in
                            this case, indices 6 through 9) uninitialized.

                            So does the assignment 't = s' cause undefined
                            behaviour by copying the values of uninitialized
                            array members?

                            Comment

                            • Robert Gamble

                              #15
                              Re: Struct assignment

                              On Jul 2, 8:02 pm, Old Wolf <oldw...@inspir e.net.nzwrote:
                              On Jul 3, 2:59 am, Eric Sosman <Eric.Sos...@su n.comwrote:
                              >
                              The compiler *may* do this, but it is not required
                              to copy padding bytes and padding bits. Assignment only
                              copies the value; padding bytes and bits are not part of
                              the value of a struct, and might not be copied -- as they
                              would be if memcpy() or an equivalent were used.
                              >
                              Something that just occurred to me. Say we
                              have:
                              struct S
                              {
                              char s[10];
                              };
                              >
                              struct S s = { "hello" };
                              struct S t;
                              t = s;
                              >
                              Initializing an automatic array from a string
                              literal is only guaranteed to initialize the
                              characters of the string and the null terminator
                              - it could leave subsequent array entries (in
                              this case, indices 6 through 9) uninitialized.
                              Actually you are wrong about that, 9899:1999 ยง6.7.8p21:

                              "If there are fewer initializers in a brace-enclosed list than there
                              are elements or members
                              of an aggregate, or fewer characters in a string literal used to
                              initialize an array of known
                              size than there are elements in the array, the remainder of the
                              aggregate shall be
                              initialized implicitly the same as objects that have static storage
                              duration."

                              Robert Gamble

                              Comment

                              Working...