copy structs unto structs?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > copy structs unto structs?




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    copy structs unto structs?  
atv


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-06 12:30 PM

Is it possible to copy a struct ptr unto a struct ptr like for example:
*structptr=*structptr;

so i would have an exact copy of all the values?

i ask this because when getting a ptr to shared memory access i wonder
how to do this.
i can't do shmdataptr=struct_ptr because that would destroy/replace my
shm pointer.

It's not necessary to write out all the values in a struct and put that
in there is it ?






[ Post a follow-up to this message ]



    Re: copy structs unto structs?  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-06 12:30 PM

atv <alef@xs4all.nl> writes:

> Is it possible to copy a struct ptr unto a struct ptr like for example:
> *structptr=*structptr;
>
> so i would have an exact copy of all the values?
>
> i ask this because when getting a ptr to shared memory access i wonder
> how to do this.
> i can't do shmdataptr=struct_ptr because that would destroy/replace my
> shm pointer.
>
> It's not necessary to write out all the values in a struct and put
> that in there is it ?

AFAIK, in C, you cannot copy a structure like this.


You could try to copy the the memory block, but I'm not sure it's
entirely portable.

struct data* src;
struct data* dst;

memcpy(dst,src,sizeof(*src));


AFAIK, the only portable way in C to copy a structure, is to copy each field
:

void copy_data(struct data* dst,struct data* src){
dst->char_field=src->char_field;
dst->short_field=src->short_field;
dst->int_field=src->int_field;
dst->float_field=src->fload_field;
copy_other_struct(&(dst->other_struct),&(src->other_struct));
switch(src->union_selector){
case 0: dst->union_field.char_field=src->union_field.char_field; break;
case 1: dst->union_field.short_field=src->union_field.short_field; break;
case 2: dst->union_field.int_field=src->union_field.int_field; break;
case 3: copy_yet_another_struct(&(dst->union_field.another_struct),
&(dst->union_field.another_struct)); break;
/* ... */
}
dst->shared_pointer=src->shared_pointer;
dst->deep_pointer=new_deep(src->deep_pointer);
/* for example: */
dst->some_immutable_string=src->some_immutable_string;
dst->mutable_string=newstr(src->mutable_string);
/* ... */
}


--
__Pascal Bourguignon__                     http://www.informatimago.com/
Litter box not here.
You must have moved it again.
I'll poop in the sink.





[ Post a follow-up to this message ]



    Re: copy structs unto structs?  
Rainer Temme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-06 12:30 PM

atv wrote:

> Is it possible to copy a struct ptr unto a struct ptr like for example:
> *structptr=*structptr;

Albeit unrelated to unix ... yes this is a valid statement in
C language, and it does copy from the memorylocation the right-pointer
points-to to the memorylocation the left-pointer points-to

> so i would have an exact copy of all the values?

Yes ... but remember, if some struct members are pointers, only
the pointer is copied, not the location these pointers are pointing
to.

> i ask this because when getting a ptr to shared memory access i wonder
> how to do this.
> i can't do shmdataptr=struct_ptr because that would destroy/replace my
> shm pointer.

yes, as said, you can do a

*shmdataptr = *struct_ptr ;

or a

*shmdataptr = struct ;

or a

 memcpy(shmdataptr,struct_ptr,sizeof(*shm
dataptr));

> It's not necessary to write out all the values in a struct and put that
> in there is it ?

No, but again, pointers (as members of the struct) will still point to
the same memorylocation.

Rainer






[ Post a follow-up to this message ]



    Re: copy structs unto structs?  
Rainer Temme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-06 12:30 PM

Pascal Bourguignon schrieb:
> AFAIK, in C, you cannot copy a structure like this.

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

$cat y.c
typedef struct str_x
{
int a;
char *b;
int c;
} X;

void printX(X *x)
{
printf("X(%p): a=%d b=[%p/%s] c=%d\n",
x,x->a,
x->b, x->b ? x->b : "",
x->c);
}

int main(int c,char **v)
{
X *p1,*p2;

p1=malloc(sizeof(X));
p2=malloc(sizeof(X));

p1->a=1; p1->b="a"; p1->c=2;

printX(p1);
*p2=*p1;
printX(p2);
return(0);
}

$ y
X(0x804a008): a=1 b=[0x8048605/a] c=2
X(0x804a018): a=1 b=[0x8048605/a] c=2

Rainer





[ Post a follow-up to this message ]



    Re: copy structs unto structs?  
Maxim Yegorushkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-06 12:30 PM


atv wrote:

> Is it possible to copy a struct ptr unto a struct ptr like for example:
> *structptr=*structptr;
> so i would have an exact copy of all the values?

It is.

struct S
{
int i;
double d;
};

int main()
{
struct S a = {0,0}, b = {0,0}, *pa = &a, *pb = &b;

a = b;
*pa = *pb; // the effect is the same as a = b

return 0;
}


> i ask this because when getting a ptr to shared memory access i wonder
> how to do this.
> i can't do shmdataptr=struct_ptr because that would destroy/replace my
> shm pointer.

To reassign a value pointed to by ptr, you do *ptr = x;. To reassign
the pointer itself you do ptr = y;.

> It's not necessary to write out all the values in a struct and put that
> in there is it ?

In C, struct's are as assignable as int's are. Just copy structs as you
would copy an int.






[ Post a follow-up to this message ]



    Re: copy structs unto structs?  
atv


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-06 12:30 PM

> Ok, thanks everyone!
gr,
Alef






[ Post a follow-up to this message ]



    Re: copy structs unto structs?  
Casper H.S. Dik


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-06 12:30 PM

"Maxim Yegorushkin" <maxim.yegorushkin@gmail.com> writes:

>In C, struct's are as assignable as int's are. Just copy structs as you
>would copy an int.

Indeed; only in the very early incantations of C (K&R, first edition),
struct copying was not well-defined (it worked on some but not all compilers
)

Casper






[ Post a follow-up to this message ]



    Re: copy structs unto structs?  
SM Ryan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-06 06:22 PM

# *shmdataptr = *struct_ptr ;
#
# or a
#
# *shmdataptr = struct ;

#  memcpy(shmdataptr,struct_ptr,sizeof(*shm
dataptr));

Assigning structs or unions is not exactly the same as memcpy.
The compiler is allowed to put filler bytes into a struct for
alignment. memcpy, memcmp, etc effect all bytes in the struct
whether filler or field. Struct assignments might skip filler
bytes. This can byte you in the behind if you're playing type
bending games with union.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
So basically, you just trace.





[ Post a follow-up to this message ]



    Re: copy structs unto structs?  
Rainer Temme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-06 06:22 PM

SM Ryan wrote:
> # *shmdataptr = *struct_ptr ;
> #
> # or a
> #
> # *shmdataptr = struct ;
>
> #  memcpy(shmdataptr,struct_ptr,sizeof(*shm
dataptr));
>
> Assigning structs or unions is not exactly the same as memcpy.
> The compiler is allowed to put filler bytes into a struct for
> alignment. memcpy, memcmp, etc effect all bytes in the struct
> whether filler or field. Struct assignments might skip filler
> bytes. This can byte you in the behind if you're playing type
> bending games with union.

Can you supply an example for this?

Rainer





[ Post a follow-up to this message ]



    Re: copy structs unto structs?  
shakahshakah@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-18-06 12:23 AM

On Nov 17, 2:10 pm, Rainer Temme <Rainer.Te...@NoSpam.Siemens.Com>
wrote:
> SM Ryan wrote: 
> 
> 
>
> Rainer

I encountered this recently, something along the lines of:

struct A {
int n ;
char[1] ch ;
} ;

struct B {
int n ;
char[1] ch ;
char[2] ch2 ;
} ;

func(B *b, A *a) {
memset(b, 0, sizeof(B)) ;
*((A *) b) = *a ;
// b->ch2 probably has garbage in it from A's padding
}






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 06:27 PM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register