473,790 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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...woul d
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),(voi d *)buff,sizeof(s truct 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 3076
ra**********@gm ail.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...woul d
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),(voi d *)buff,sizeof(s truct 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_Keit h) 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************ **********@h54g 2000cwb.googleg roups.com>,
<ra**********@g mail.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.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <11************ **********@h54g 2000cwb.googleg roups.com>,
<ra**********@g mail.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_Keit h) 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.nr c-cnrc.gc.ca (Walter Roberson) writes:
>In article <11************ **********@h54g 2000cwb.googleg roups.com>,
<ra**********@g mail.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.orgw rote:
>ro******@ibd.n rc-cnrc.gc.ca (Walter Roberson) writes:
>>In article <11************ **********@h54g 2000cwb.googleg roups.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
2523
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 trans_handle, ref int stmt_handle,
26
1941
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 ints. The size of the arrays would depend on some external value (going from 0 to around 1000 max). I would have an array of max 255 of these classes/structs (in most cases it will be less then 5 however) Since there's no real business logic my...
7
5535
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 FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, pJob As Byte, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Lon Which I got from the API viewer that comes with VB 6. I have tried to convert it to the following ...
19
1642
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 in C# will be the same - i was wrong.... or if in C# is the same as in java please tell me ;-) I have an ArrayList names ls : // Point is a class System.Draw....
13
2315
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 property(variable). The string would be a reference type being contained in a value type. Would this filter up and cause the stack to now be a reference type placed on the heap or would it still be on the stack with a pointer used internally to point to a...
5
1364
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 certain default values to it. I have assigned this struct to a TreeView node via the Tag field. In my "AfterSelect" processing call, I am casting the node tag value to my "Info" struct. When I look at the fields they are set to the default values...
24
2019
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, city, state, zipcode etc I thought of making a base class - BusinessEntity (with all of the
7
6444
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 now thown out of the array released properly by the CLI?
12
5542
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: typedef struct {
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10412
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10142
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.