Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > copy a struct

Reply
Thread Tools

copy a struct

 
 
Michael
Guest
Posts: n/a
 
      03-11-2005
Hi all,

I want to copy a structure, but the only thing
I have is a pointer to it.

Whats good here? Because the structure contains no pointers,
but arrays and variables I thought of doing a memcpy:

#include <stdio.h>
#include <string.h>

struct test {
int a;
int b[2];
};

int main (void)
{
struct test t1, t2;
struct test *pt1;

pt1 = &t1;
t1.a = 1; t1.b[0] = 42; t1.b[1] = 173;

memcpy(&t2, pt1, sizeof(t2) );
printf ( "t1:a=%d, b_0=%d, b_1=%d\n", t1.a, t1.b[0], t1.b[1]);
printf ( "t2:a=%d, b_0=%d, b_1=%d\n", t2.a, t2.b[0], t2.b[1]);

return 0;
}
> gcc test.c -Wall
> a.out

t1:a=1, b_0=42, b_1=173
t2:a=1, b_0=42, b_1=173

It works, but I am a little confortable with this
solution. Would it be better to use a function, which
copy each variable?
> gcc test.c -Wall
> a.out

t1:a=1, b_0=42, b_1=173
t2:a=1, b_0=42, b_1=173


Thanks in advance
Michael

 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      03-11-2005
Michael <(E-Mail Removed)> wrote:
> I want to copy a structure, but the only thing
> I have is a pointer to it.


> Whats good here? Because the structure contains no pointers,
> but arrays and variables I thought of doing a memcpy:


> #include <stdio.h>
> #include <string.h>


> struct test {
> int a;
> int b[2];
> };


> int main (void)
> {
> struct test t1, t2;
> struct test *pt1;


> pt1 = &t1;
> t1.a = 1; t1.b[0] = 42; t1.b[1] = 173;


> memcpy(&t2, pt1, sizeof(t2) );
> printf ( "t1:a=%d, b_0=%d, b_1=%d\n", t1.a, t1.b[0], t1.b[1]);
> printf ( "t2:a=%d, b_0=%d, b_1=%d\n", t2.a, t2.b[0], t2.b[1]);


> return 0;
> }
> > gcc test.c -Wall
> > a.out

> t1:a=1, b_0=42, b_1=173
> t2:a=1, b_0=42, b_1=173


> It works, but I am a little confortable with this
> solution. Would it be better to use a function, which
> copy each variable?


You're looking for too complicated a solution. All you need is

t2 = *pt1;

and no memcpy() or other function calls. Note that structures are
"first class cititens" of C, you can copy them as a whole, pass
them to functions by value or return them from functions etc.

Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
 
bjrnove
Guest
Posts: n/a
 
      03-11-2005
Hi.

memcpy is a good solution in this case since you wouldn't have to
change anything to add something else into the struct. Another thing
you can do is this:

t2 = *pt1;

Then it would be up to the compiler how to copy the items of the
struct.

--
bjrnove

 
Reply With Quote
 
Goran Larsson
Guest
Posts: n/a
 
      03-11-2005
In article <d0s2e5$s7l$(E-Mail Removed)-bs.de>, Michael <(E-Mail Removed)> wrote:

> Whats good here?


A simple assignment?

| #include <stdio.h>
| #include <string.h>
|
| struct test {
| int a;
| int b[2];
| };
|
| int main (void)
| {
| struct test t1, t2;
struct test t3;
| struct test *pt1;
|
| pt1 = &t1;
| t1.a = 1; t1.b[0] = 42; t1.b[1] = 173;
|
| memcpy(&t2, pt1, sizeof(t2) );
t3 = *pt1;
| printf ( "t1:a=%d, b_0=%d, b_1=%d\n", t1.a, t1.b[0], t1.b[1]);
| printf ( "t2:a=%d, b_0=%d, b_1=%d\n", t2.a, t2.b[0], t2.b[1]);
printf ( "t3:a=%d, b_0=%d, b_1=%d\n", t3.a, t3.b[0], t3.b[1]);
|
| return 0;
| }
| > gcc test.c -Wall
| > a.out
| t1:a=1, b_0=42, b_1=173
| t2:a=1, b_0=42, b_1=173
t3:a=1, b_0=42, b_1=173

> It works, but I am a little confortable with this
> solution. Would it be better to use a function, which
> copy each variable?


Why are you trying to make it more complicated than it is?

--
Göran Larsson http://www.mitt-eget.com/
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      03-11-2005
(E-Mail Removed)-berlin.de wrote:
>
> Michael <(E-Mail Removed)> wrote:
> > I want to copy a structure, but the only thing
> > I have is a pointer to it.

>
> > Whats good here? Because the structure contains no pointers,
> > but arrays and variables I thought of doing a memcpy:

>
> > #include <stdio.h>
> > #include <string.h>

>
> > struct test {
> > int a;
> > int b[2];
> > };

>
> > int main (void)
> > {
> > struct test t1, t2;
> > struct test *pt1;

>
> > pt1 = &t1;
> > t1.a = 1; t1.b[0] = 42; t1.b[1] = 173;

>
> > memcpy(&t2, pt1, sizeof(t2) );
> > printf ( "t1:a=%d, b_0=%d, b_1=%d\n", t1.a, t1.b[0], t1.b[1]);
> > printf ( "t2:a=%d, b_0=%d, b_1=%d\n", t2.a, t2.b[0], t2.b[1]);

>
> > return 0;
> > }
> > > gcc test.c -Wall
> > > a.out

> > t1:a=1, b_0=42, b_1=173
> > t2:a=1, b_0=42, b_1=173

>
> > It works, but I am a little confortable with this
> > solution. Would it be better to use a function, which
> > copy each variable?

>
> You're looking for too complicated a solution. All you need is
>
> t2 = *pt1;
>
> and no memcpy() or other function calls. Note that structures are
> "first class cititens" of C, you can copy them as a whole, pass
> them to functions by value or return them from functions etc.


I would put structures a step below first class.
You can't use any of the equality or relational operators
with structures.

--
pete
 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      03-11-2005
> Why are you trying to make it more complicated than it is?
I don't know, I get it thanks!

Thanks to all of you!
Michael

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      03-11-2005
Michael wrote on 11/03/05 :
> I want to copy a structure, but the only thing
> I have is a pointer to it.


As long as the type is known and complete, you don't need more.


f (T *p)
{
T s = *p;
}

end of the story.

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

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++

 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      03-11-2005

<(E-Mail Removed)-berlin.de> wrote
>
> You're looking for too complicated a solution. All you need is
>
> t2 = *pt1;
>
> and no memcpy() or other function calls. Note that structures are
> "first class cititens" of C, you can copy them as a whole, pass
> them to functions by value or return them from functions etc.
>

This is on my hit list of features that ought to be removed from the
language. It breaks the rule that no C statement may compile to more than a
few assembly instructions.


 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      03-11-2005
On Fri, 11 Mar 2005 22:29:08 -0000, "Malcolm"
<(E-Mail Removed)> wrote:

>
><(E-Mail Removed)-berlin.de> wrote
>>
>> You're looking for too complicated a solution. All you need is
>>
>> t2 = *pt1;
>>
>> and no memcpy() or other function calls. Note that structures are
>> "first class cititens" of C, you can copy them as a whole, pass
>> them to functions by value or return them from functions etc.
>>

>This is on my hit list of features that ought to be removed from the
>language. It breaks the rule that no C statement may compile to more than a
>few assembly instructions.
>

That rule would seem to be heavily dependent on the machine's
instruction set

On many machines, this would be loading a source pointer, a
destination pointer, a count, and one instruction.

--
Al Balmer
Balmer Consulting
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      03-13-2005

"Alan Balmer" <(E-Mail Removed)> wrote
> >This is on my hit list of features that ought to be removed from the
> >language. It breaks the rule that no C statement may compile to more
> >than a few assembly instructions.
> >

> That rule would seem to be heavily dependent on the machine's
> instruction set
>

The other place the rule breaks down is where the compiler writer decides to
implement reasonable precision floating-point on a small processor with no
FPU. There's no easy way round this, since if you try to use functions
called things like flt_div() code rapidly becomes unreadable.

However you don't want a seemingly innocuous instruction like "x = y"
actually accounting for a narrow bottleneck in your code. When you gobble
lots of processor power, you wan't a warning.
>
> On many machines, this would be loading a source pointer, a
> destination pointer, a count, and one instruction.
>

What we are really interested in is the execution speed, not the number of
instructions.
It wouldn't be too difficult to build a massively parallel memcpy(), but I
don't think any are in common use. What you do have sometimes is the looping
logic implemented on chip, which speeds things up.

A lot of machines also have DMA engines, which can copy long structures in
parallel with main program flow. So you need to call DMcopy(), do a bit of
logic, and then retrieve your results. This is of course really awkward to
program, and only useful for huge structures.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments