473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

question on structs

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
5 3043
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Carlos Guzmán Álvarez | last post by:
Hello: I'm trying to execute a function of a unmanaged dll using PInvoke, i have definied the function as: public static extern int isc_dsql_prepare( int status_vector, ref int...
26
by: phoenix | last post by:
Hello, I've got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I'm talking about 10 bools, 20 ints and 2 arrays of...
7
by: Kevin | last post by:
Hi al I have an interesting question.... I am working witha Win API this is the Function Public Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal...
19
by: Xandau | last post by:
hello all, i wotk with java every day but last time i have interested in C#. everything goes great except one thing... in java everything is a reference (except plain types) so i thought that...
13
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string...
5
by: Brian | last post by:
I am "learning" C# and have run into a problem that, though I can work around it, I would like to know what the *right* way to handle the issue is. I have created an "Info" struct and assigned...
24
by: Kalpesh | last post by:
Hello All, Please help validate this design problem Assume that I have several entities in my project (eg Supplier, Customer etc). All of them save several common properties - name, address,...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
12
by: Milux | last post by:
Hi All, This question has to do with interface design. Suppose I have an translation tool. It can translates structs from type "general" to other types and then does some processing. Example:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.