Sign in to follow this  
DenizS

No way to send structs

Recommended Posts

DenizS    100
I have billions of sizeof(),send,recv problems. It almost made me cancel the project. It seems structs have weird sizeofs. I can't even send a struct with a char and int correctly.



struct joingame {
char id;
int gameid;
};

joingame *MSG1=(joingame*)malloc(sizeof(joingame));
MSG1->id='1';
MSG1->gameid=0;

sizeof(joingame) returns 4. It must have been 5, and when i send MSG1, server gets the char id properly but the gameid becomes something like -8543532. I have read many things and searched this a lot, i have heard about data alignment for structs, big little endian etc. But i couldn't find any solutions. When i malloc, send and recv MSG1 with 8 bytes, everything goes ok. But i don't want to set the bytes customly, if sizeof() doesn't work, what works ? And how can i [b]correctly [/b]a struct with different data types ?

Share this post


Link to post
Share on other sites
magic_man    54
[quote name='artariel' timestamp='1301501709' post='4792218']
I have billions of sizeof(),send,recv problems. It almost made me cancel the project. It seems structs have weird sizeofs. I can't even send a struct with a char and int correctly.
struct joingame {
char id;
int gameid;
};

sizeof(joingame) returns 4. It must have been 5,
[/quote]


I personally would find it very strange if sizeof for that structure returned 4, this would mean there is no padding and an int is 3 bytes. If the structure used a compiler directive to pack the structure then I would expect it to be 5 bytes on some platforms.


[quote]When i malloc, send and recv MSG1 with 8 bytes, everything goes ok[/quote]
This is what I would maybe expect, where int is 4 bytes and 3 bytes of padding is being added.

[quote]And how can i [b]correctly [/b]a struct with different data types ?[/quote]
I am sure there is something about this in the FAQ but I would either use a compiler directive on both ends to pack the structure removing padding bytes and also use fixed sized types or send each sized type (in this case char and int32) as a series on chars not as a structure,

Share this post


Link to post
Share on other sites
Moomin    332
[quote name='artariel' timestamp='1301501709' post='4792218']
sizeof(joingame) returns 4. It must have been 5,
[/quote]

This sounds suspiciously like you were getting the size of a pointer to the struct, i.e. sizeof(joingame*), which would of course be 4. I suggest you carefully check you sizeof calls.

Share this post


Link to post
Share on other sites
wood_brian    193
[quote name='magic_man' timestamp='1301502287' post='4792221']
[quote]And how can i [b]correctly [/b]a struct with different data types ?[/quote]
I am sure there is something about this in the FAQ but I would either use a compiler directive on both ends to pack the structure removing padding bytes and also use fixed sized types or send each sized type (in this case char and int32) as a series on chars not as a structure,
[/quote]

I agree with the advice to use types that have the same size on different platforms. Packing may work for you, but that's kind of a fragile approach. It would probably be better to use a library that helps you marshall structs. I think s11n.net has something like that. I have a marshalling framework also, but it is geared toward C++ users.

Brian Wood
Ebenezer Enterprises
[url="http://webEbenezer.net"]http://webEbenezer.net[/url]

Share this post


Link to post
Share on other sites
hplus0603    11347
[quote name='artariel' timestamp='1301501709' post='4792218']
I have billions of sizeof(),send,recv problems. It almost made me cancel the project. It seems structs have weird sizeofs. I can't even send a struct with a char and int correctly.
[/quote]

It sounds like what you should do is actually learn how structs, packing/padding, memory layout, and sizeof() works.


If you're currently not able to implement a distributed simulation over a network because of this, then it probably does make sense to focus on the necessary learning first.

There are some pointers in the FAQ for this forum (accessible from the top of the topic list).

There are also lots of resources on how C/C++ lays out things in memory in various C/C++ tutorials and FAQs around the net. Unfortunately, many other tutorials are written by people who are at the same time trying to learn, so quality varies.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this