question on structs

R

ravi.sathyam

Hey,
So say I have a sockaddr_in struct stored in a packet which I receive
from my udp socket.....and it is stored within a certain offset into
this packet (which is basically a char array). Currently, the first
four bytes store an ID information and the next sizeof(struct
sockaddr_in) bytes store this struct.

Now, say that i declare a struct sockaddr_in temp_addr variable...would
the following lines be valid??

//we fill up char* buff using the recvfrom function
//we declare following struct
struct sockaddr_in addr;

memcpy ( (void *)&(addr),(void *)buff,sizeof(struct sockaddr_in));

So basically, my question is are structs stored contiguously in memory?
So can I copy entire structs from one location to another? Or do I have
to copy individual values of the struct (such as port number etc) from
buff to addr?

I'm sorry if my question is not making sense, but I can elaborate if
needed...


Thanks,
Ravi Sathyam
 
K

Keith Thompson

So say I have a sockaddr_in struct stored in a packet which I receive
from my udp socket.....and it is stored within a certain offset into
this packet (which is basically a char array). Currently, the first
four bytes store an ID information and the next sizeof(struct
sockaddr_in) bytes store this struct.

Now, say that i declare a struct sockaddr_in temp_addr variable...would
the following lines be valid??

//we fill up char* buff using the recvfrom function
//we declare following struct
struct sockaddr_in addr;

memcpy ( (void *)&(addr),(void *)buff,sizeof(struct sockaddr_in));

So basically, my question is are structs stored contiguously in memory?
So can I copy entire structs from one location to another? Or do I have
to copy individual values of the struct (such as port number etc) from
buff to addr?

Sockets are non-standard, but your question is really about the
behavior of structs in general, so it's topical.

Yes, structures are stored contiguously in memory, and you can safely
copy entire structs with memcpy().

In most cases, memcpy() is unnecessary (you can just use a simple
assignment), but in this case it's not guarantee that the struct is
properly aligned.
 
W

Walter Roberson

So say I have a sockaddr_in struct

What's a sockaddr_in struct ?
stored in a packet

What's a packet?
which I receive from my udp socket

What is udp? What is a socket?

So basically, my question is are structs stored contiguously in memory?

Not with any certainty. The compiler may use any internal padding
it needs in order to align things for the architectural rules.
So can I copy entire structs from one location to another? Or do I have
to copy individual values of the struct (such as port number etc) from
buff to addr?

That's a different question -- that's structure assignment.
Structure assignment is supported in ANSI C.
 
K

Keith Thompson

Not with any certainty. The compiler may use any internal padding
it needs in order to align things for the architectural rules.

You're right, I missed that point in my response.
That's a different question -- that's structure assignment.
Structure assignment is supported in ANSI C.

Assignment may or may not work depending on the alignment (the
structure is in an array of bytes), but memcpy() should work reliably.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Keith said:
You're right, I missed that point in my response.


Assignment may or may not work depending on the alignment (the
structure is in an array of bytes), but memcpy() should work reliably.

An important heads up the OP must be aware of is wether the host sending
the struct is compatible with the receiving one. A struct sockaddr_in
might differ between the sending and receiving platform if these differ.
 
G

Giorgos Keramidas

You're right, I missed that point in my response.


Assignment may or may not work depending on the alignment (the
structure is in an array of bytes), but memcpy() should work reliably.

Since networking is involved though, different compiler versions may
have been used on the 'sides' of the network connection. Different
compilers may have different packing/alignment ideas about the same
struct. This will make it hard to get things to work right, even if
someone uses such `unportable' features like __packed or other
compiler-specific tricks.

IMHO, it's just not a good idea to pass around binary copies of
structures through a network connection. Some sort of 'serialization
technique' for the structure members is needed. Then functions which
can convert from a struct sockaddr_in to its `network representation'
(whatever this may be) and from its network representation back to a
`struct sockaddr_in' on both sides of the connection can make things
much more portable.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top