Jump to content

  • Log In with Google      Sign In   
  • Create Account

No way to send structs


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 artariel   Members   -  Reputation: 100

Like
0Likes
Like

Posted 30 March 2011 - 10:15 AM

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 ?

Sponsor:

#2 magic_man   Members   -  Reputation: 54

Like
0Likes
Like

Posted 30 March 2011 - 10:24 AM

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

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 ?

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!

#3 Moomin   Members   -  Reputation: 332

Like
3Likes
Like

Posted 30 March 2011 - 11:06 AM

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.

#4 artariel   Members   -  Reputation: 100

Like
0Likes
Like

Posted 30 March 2011 - 11:11 AM

whoops you may be right, i think i am trying to get pointer size, i'll check when i'm at home.

#5 wood_brian   Banned   -  Reputation: 197

Like
0Likes
Like

Posted 30 March 2011 - 11:58 AM

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,


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

#6 hplus0603   Moderators   -  Reputation: 5302

Like
-1Likes
Like

Posted 31 March 2011 - 12:10 PM

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 };

#7 artariel   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 April 2011 - 02:15 AM

problem solved. as you have said, i had been trying to send sizeof the pointer. now it works correctly, thanks.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS