LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-21-2008, 03:52 PM   #16
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197

Quote:
Originally Posted by pinniped View Post
Is the __attribute__((packed)) part of the C specification or is it a gnuism?
It is GNU, not standard C.

I assumed that was OK. If the OP was even considering a GNU specific command line switch to cover up the non portable code, then a GNU specific attribute is at least better.

No way of doing that structure is portable. The alignment and packing are not even the only portability problems.

I expect you are suggesting a complete translation of the contents to the desired sequence of bytes. I don't think anything less than that could be portable to the level of the C specification.

I expect (but don't really know) that the OP doesn't really need that much portability.
 
Old 11-21-2008, 04:41 PM   #17
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
You're right, it depends on the OP's actual requirements. The code I worked on years ago was meant to be cross-platform so everything was an issue including the Endian order. We supported UNIX and XINU but dismissed NUXI and obscure historic curiosities.

So in my case, each struct that was meant to be sent over the wire had a function within it which packed all the data into an unsigned char[] and another to unpack data from an unsigned char[].
 
Old 11-21-2008, 08:43 PM   #18
linux001
LQ Newbie
 
Registered: Nov 2008
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pinniped View Post
Is the __attribute__((packed)) part of the C specification or is it a gnuism?

I don't know if the data ordering in a struct is governed by any C specs, and it's been a long time since I've used anything other than GCC, but data ordering within the struct was an issue - some compilers could shuffle around data to avoid excessive packing.

If you use any tricks to shape your struct just the way you want, make sure they're in the C specification; otherwise you're just doing contortion acts to save a little time writing the code which can actually guarantee that data is sent just the way you like.
Yes, I understand what's your concern now. But what is the solution? What is your suggestion about not use struct to carry data through socket?
 
Old 11-21-2008, 11:18 PM   #19
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
The easiest solution is not to use a struct to send data. Now that you've reminded me of some past work, one more excellent reason for not doing this is that you may not be sending the data which you think you are sending - for example:

Code:
struct blah{
  int a;
  char *a_string;
};
Now you allocate some memory, put in the string "blah blah blah - this won't work!" and assign that to 'a_string'. When you send the struct, you send the value of 'a' and the value (pointer) of 'a_string' when you probably really meant to send the string "blah blah blah - this won't work!"

So my opinion is that the *only* sensible way to avoid issues with compiler versions, padding, variable order, and so on is to write a routine which stuffs the data into an array of unsigned chars (or reads data out of such a string). It is up to you whether you consider the Endian order or not.
 
Old 11-22-2008, 07:31 AM   #20
linux001
LQ Newbie
 
Registered: Nov 2008
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pinniped View Post
The easiest solution is not to use a struct to
send data.
What's its replacement?

Quote:
Originally Posted by pinniped View Post
Now that you've reminded me of some past work, one more excellent reason for not doing this is that you may not be sending the data which you think you are sending - for example:

Code:
struct blah{
  int a;
  char *a_string;
};
Now you allocate some memory, put in the string "blah blah blah - this won't work!" and assign that to 'a_string'. When you send the struct, you send the value of 'a' and the value (pointer) of 'a_string' when you probably really meant to send the string "blah blah blah - this won't work!"

So my opinion is that the *only* sensible way to avoid issues with compiler versions, padding, variable order, and so on is to write a routine which stuffs the data into an array of unsigned chars (or reads data out of such a string). It is up to you whether you consider the Endian order or not.
we don't have pointer in our struct. Instead we use char[n] if necessary.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LiveCD(any 32bit distro) won't initialize on 64bit machine TELinux46 Linux - Newbie 1 10-20-2008 12:29 PM
Flash player in 64bit machine vladlx Linux - Software 1 06-27-2007 04:24 AM
wu-ftpd can it be installed on 64bit linux MACHINE athreyavc Linux - Server 2 06-05-2007 08:06 PM
FC5 64bit on 32 bit machine ekdya Fedora 3 09-15-2006 03:52 AM
SUSE 9.0/64bit (AMD64) KDE log in issue syx Linux - Newbie 2 02-05-2004 04:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration