No way to send structs

Started by
5 comments, last by DenizS 13 years ago
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 correctly a struct with different data types ?
Advertisement

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,



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.


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.

And how can i correctly 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,
"You insulted me!" I did not say that in the private message Tom Sloper!

sizeof(joingame) returns 4. It must have been 5,


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.
whoops you may be right, i think i am trying to get pointer size, i'll check when i'm at home.

And how can i correctly a struct with different data types ?

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
http://webEbenezer.net

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.


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.
enum Bool { True, False, FileNotFound };
problem solved. as you have said, i had been trying to send sizeof the pointer. now it works correctly, thanks.

This topic is closed to new replies.

Advertisement