Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question on using different types within a struct.

Reply
Thread Tools

Question on using different types within a struct.

 
 
Durango
Guest
Posts: n/a
 
      03-08-2012
I am looking to use structs in my program so that I can pass them as
"packets" over a network socket created within the program.

I am particularly interested in the following scenario:

typedef struct A
{
int a;
int b;
int c;
}pkt_A;

typedef struct B
{
int def;
char msg[32];
}pkt_B;

typedef struct C
{
double d;
char msg1[32];
char msg2[32];
}pkt_C;

typedef struct D
{
int code;
void *body;
}pkt_D;


Now I want struct D to act as the container for the other structs that
are created. I am wondering if this would work or if there is a better
way to do this? I am not sure if I am on the right track.

Thanks for any advice.



 
Reply With Quote
 
 
 
 
Anders Wegge Keller
Guest
Posts: n/a
 
      03-08-2012
Durango <(E-Mail Removed)> writes:

> Now I want struct D to act as the container for the other structs
> that are created. I am wondering if this would work or if there is
> a better way to do this? I am not sure if I am on the right track.


typedef struct D {
int code;
union {
pkt_A a;
pkt_B b;
pkt_C c;
}
}

This will work as long as code identifies which of the three data
structures are used.

--
/Wegge

Leder efter redundant peering af dk.*,linux.debian.*
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      03-08-2012
Durango <(E-Mail Removed)> wrote:
> I am looking to use structs in my program so that I can pass them as
> "packets" over a network socket created within the program.


First of all sending structures over the net is typically
not a good idea because a structure created on machine A,
using the exact same code, can look rather different from
one created on machine B. Even somethig simple as an int
can have different sizes on different machines, the binary
representation of the values may be different (i.e. you
may have different endian-ness on the machines). Then
the compiler may insert different amounts of "padding"
between the members of the structure - even if the archi-
tectures of machine A and B are identical, i.e. use the
same sizes and endian-ness for all types you use the
same structure created by different compiler or even
different versions of the same compiler can be different.

So sending structures from one machine to another (or
just storing data by writing a structure directly to
a file) is usually a rather bad idea (I tried that my-
self once before I understood the implictions and got
bitten by it badly

The simplest way to transmit (or store) values is
by converting them to ASCII strings. Simple, if the
data are already strings, also very simple for inte-
ger types, a bit more tricky for floating point
values since you have to consider which precision
you need.

> I am particularly interested in the following scenario:


> typedef struct A
> {
> int a;
> int b;
> int c;
> }pkt_A;


> typedef struct B
> {
> int def;
> char msg[32];
> }pkt_B;


> typedef struct C
> {
> double d;
> char msg1[32];
> char msg2[32];
> }pkt_C;


> typedef struct D
> {
> int code;
> void *body;
> }pkt_D;


This looks completely wrong - a pointer on the machine you
send this structure from has no meaning on the machine you
receive it on, so it can't be used for anything.

> Now I want struct D to act as the container for the other structs that
> are created. I am wondering if this would work or if there is a better
> way to do this? I am not sure if I am on the right track.


What do you mean by "as the container"? You have four struc-
tures of different sizes, how could be one of them with pro-
bably one of the smallest sizes be used as a "container" for
the others?

Did you intent to set the 'code' member of 'pkt_D' to some-
thing indicate the type of one of the other three and make
the 'body' member point to it and then send just this over
the network? If that's the case that won't work at all.
When the other machine gets the structure of type 'pkt_D'
(even when assuming that both machines are identical and
you used the same compiler and version on both of them
to compile the program) having a pointer on B that is
the address of something in the memory of machine A is
rather useless since the program running on B can't access
that (even if these would be two programs running on the
same machine it wouldn't work since typically, i.e. on
machines with a virtual memory system, a program can't
access the memory of another).

Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Durango
Guest
Posts: n/a
 
      03-09-2012
On Thu, 08 Mar 2012 10:28:09 +0000, Jens Thoms Toerring wrote:

> Durango <(E-Mail Removed)> wrote:
>
> Regards, Jens


Thank you for your input Jens, it makes sense now after I read your post.
After some more research I will look into using serialization method to
accomplish this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Different behaviour in different struct declaration types __PaTeR C Programming 7 01-01-2009 12:13 AM
running different plugins within a java application in different JVMs [urgent need of help] dima.alina@gmail.com Java 2 03-13-2006 06:39 PM
running different plugins within a java application in different JVMs [urgent need of help] dima.alina@gmail.com Java 3 03-09-2006 01:34 PM
running different plugins within a java application in different JVMs [urgent need of help] dima.alina@gmail.com Java 3 02-23-2006 01:07 PM
running different plugins within a java application in different JVMs dima.alina@gmail.com Java 0 02-22-2006 08:28 AM



Advertisments