Structure copy rule in c

Discussion in 'C Programming' started by Shivanand Kadwadkar, Dec 25, 2010.

  1. When structure is assigned, passed, or returned, the copying is done
    monolithically. This means that the copies of any pointer fields will
    point to the same place as the original. In other words, the data
    pointed to is not copied.
    But in case normal of normal variable they are copied to different
    location.

    Any idea why there is a partiality between copying normal variable and
    pointer.
    ------------------------------------------------------------
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    struct my
    {
    int *data1;
    int data2;
    }a,b;
    a.data1=malloc(sizeof(int));
    *a.data1=1;
    a.data2=1;
    b=a;
    printf("\n %d %d", *b.data1,a.data2);
    *b.data1=2;
    b.data2=2;
    printf("\n %d %d", *a.data1,a.data2);
    printf("\n %d %d", *b.data1,b.data2);
    }
    Output:
    1 1
    2 1
    2 2
    --------------------------------------------
    Shivanand Kadwadkar, Dec 25, 2010
    #1
    1. Advertising

  2. Shivanand Kadwadkar <> writes:
    > When structure is assigned, passed, or returned, the copying is done
    > monolithically. This means that the copies of any pointer fields will
    > point to the same place as the original. In other words, the data
    > pointed to is not copied.


    Right. In other words, struct assignment does a "shallow copy",
    not a "deep copy".

    > But in case normal of normal variable they are copied to different
    > location.


    I'm not sure what you mean. Assignment for any object type simply
    copies the contents of the object.

    > Any idea why there is a partiality between copying normal variable and
    > pointer.
    > ------------------------------------------------------------
    > #include<stdio.h>
    > #include<stdlib.h>
    > int main()
    > {
    > struct my
    > {
    > int *data1;
    > int data2;
    > }a,b;
    > a.data1=malloc(sizeof(int));
    > *a.data1=1;


    You've created an object of type int and assigned the value 1 to it.
    This object typically exists on "the heap"; it's not part of any
    other object. (The standard doesn't use the term "heap", but it's
    a common term for the region of memory managed by malloc and free.)

    > a.data2=1;
    > b=a;


    a and b are two objects of type "struct my". a.data1 and b.data1
    both point to the int object you created before. a.data2 and
    b.data2 are two distinct objects of type int.

    > printf("\n %d %d", *b.data1,a.data2);
    > *b.data1=2;


    This modifies the heap object you created above. Remember
    that both a.data1 and b.data1 point to this object.

    > b.data2=2;


    This modifies b.data2 which is part of b. It has no effect
    on a.data2.

    > printf("\n %d %d", *a.data1,a.data2);
    > printf("\n %d %d", *b.data1,b.data2);
    > }
    > Output:
    > 1 1
    > 2 1
    > 2 2


    That's the output I'd expect. Why do you find it strange?

    Finally, some style points (not relevant to the question you asked).
    Your code would be easier to read if you made better use of white
    space. The correct declaration for main is "int main(void)", not
    "int main()" (though "int main()" is probably ok). When calling
    malloc(), using the size of the allocated object rather than the
    size of the type makes your code easier to maintain; you don't
    have to change the call if the type changes later (e.g., "ptr =
    malloc(sizeof *ptr);"). malloc() signals failure by returning
    a null pointer; you should always check for this. You should
    print newline characters *after* each line of output, not before.
    Since main returns a result of type int, you should return a result
    (C99 implicitly returns 0 for you, but it's still a good idea to
    be explicit).

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

    int main(void)
    {
    struct my
    {
    int *data1;
    int data2;
    } a, b;
    a.data1 = malloc(sizeof *a.data1);
    if (a.data1 == NULL) {
    exit(EXIT_FAILURE);
    }
    *a.data1 = 1;
    a.data2 = 1;
    b = a;
    printf("%d %d\n", *b.data1, a.data2);
    *b.data1 = 2;
    b.data2 = 2;
    printf("%d %d\n", *a.data1, a.data2);
    printf("%d %d\n", *b.data1, b.data2);
    return 0;
    }

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Dec 25, 2010
    #2
    1. Advertising

  3. Shivanand Kadwadkar

    Seebs Guest

    On 2010-12-25, Shivanand Kadwadkar <> wrote:
    > When structure is assigned, passed, or returned, the copying is done
    > monolithically. This means that the copies of any pointer fields will
    > point to the same place as the original. In other words, the data
    > pointed to is not copied.
    > But in case normal of normal variable they are copied to different
    > location.


    Right. In both cases, the actual struct member is copied to a new
    location. When it's a pointer, the pointer really is copied -- it's
    just the stuff pointed to that isn't copied.

    > Any idea why there is a partiality between copying normal variable and
    > pointer.


    There isn't. The normal variables and the pointers are both copied.

    Also, "partiality" is not the word you want here, I'm guessing you
    wanted "difference".

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Dec 25, 2010
    #3
  4. Shivanand Kadwadkar

    Katie Guest

    On Dec 25, 4:50 pm, Shivanand Kadwadkar
    <> wrote:
    > When structure is assigned, passed, or returned, the copying is done
    > monolithically. This means that the copies of any pointer fields will
    > point to the same place as the original. In other words, the data
    > pointed to is not copied.
    > But in case normal of normal variable they are copied to different
    > location.



    The pointer is a variable like int, char, etc. and the pointer is
    copied identically. It's just a byte copy. There's no logic which says
    "this is a pointer, instead of copying the byte value copy what it
    points to instead".

    There are lots of reasons for this, imagine you had a struct which
    contained a pointer pointing to the struct, copying it would cause an
    infinite loop.

    Katie
    --
    CoderStack
    http://www.coderstack.co.uk/c-jobs
    The Software Developer Job Board
    Katie, Dec 26, 2010
    #4
  5. Shivanand Kadwadkar

    arnuld Guest

    > On Dec 25, 9:50 pm, Shivanand Kadwadkar <> wrote:

    > ..SNIP...
    > Any idea why there is a partiality between copying normal variable and
    > pointer.



    I did not know that C standard keeps a partiality between variables
    and pointers. Ah... C'mon pointers have to be superior ;)
    arnuld, Dec 27, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    2
    Views:
    1,193
  2. Replies:
    0
    Views:
    1,337
  3. Leo Nunez
    Replies:
    3
    Views:
    1,177
    Neil Kurzman
    Feb 9, 2005
  4. utab
    Replies:
    9
    Views:
    320
    Richard Herring
    Jun 26, 2006
  5. Shivanand Kadwadkar

    Structure byte padding rule

    Shivanand Kadwadkar, Feb 24, 2011, in forum: C Programming
    Replies:
    11
    Views:
    596
    Tim Rentsch
    Mar 11, 2011
Loading...

Share This Page