Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Structure copy rule in c (http://www.velocityreviews.com/forums/t740844-structure-copy-rule-in-c.html)

Shivanand Kadwadkar 12-25-2010 04:50 PM

Structure copy rule in c
 
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
--------------------------------------------

Keith Thompson 12-25-2010 05:36 PM

Re: Structure copy rule in c
 
Shivanand Kadwadkar <shivanand.kadwadkar@gmail.com> 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) kst-u@mib.org <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"

Seebs 12-25-2010 07:17 PM

Re: Structure copy rule in c
 
On 2010-12-25, Shivanand Kadwadkar <shivanand.kadwadkar@gmail.com> 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 / usenet-nospam@seebs.net
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.

Katie 12-26-2010 02:14 AM

Re: Structure copy rule in c
 
On Dec 25, 4:50*pm, Shivanand Kadwadkar
<shivanand.kadwad...@gmail.com> 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

arnuld 12-27-2010 11:45 AM

Re: Structure copy rule in c
 
> On Dec 25, 9:50*pm, Shivanand Kadwadkar <shivanand.kadwad...@gmail.com> 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 ;)



All times are GMT. The time now is 01:16 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.