By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

question on structs

P: n/a
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

Nov 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ra**********@gmail.com writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 8 '06 #2

P: n/a
In article <11**********************@h54g2000cwb.googlegroups .com>,
<ra**********@gmail.comwrote:
>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.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Nov 8 '06 #3

P: n/a
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <11**********************@h54g2000cwb.googlegroups .com>,
<ra**********@gmail.comwrote:
[...]
>>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.
You're right, I missed that point in my response.
>>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.
Assignment may or may not work depending on the alignment (the
structure is in an array of bytes), but memcpy() should work reliably.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 8 '06 #4

P: n/a
Keith Thompson wrote:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>In article <11**********************@h54g2000cwb.googlegroups .com>,
<ra**********@gmail.comwrote:
[...]
>>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.

You're right, I missed that point in my response.
>>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.

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.
Nov 8 '06 #5

P: n/a
On Wed, 08 Nov 2006 05:54:39 GMT, Keith Thompson <ks***@mib.orgwrote:
>ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>>In article <11**********************@h54g2000cwb.googlegroups .com>,
<ra**********@gmail.comwrote:
[...]
>>>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.

You're right, I missed that point in my response.
>>>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.

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.

Nov 18 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.