LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-05-2006, 02:45 PM   #1
guestgulkan
LQ Newbie
 
Registered: Nov 2005
Posts: 3

Rep: Reputation: 0
Question Prevent compiler padding structures out to WORD boundaries


Hi,
If you have a structure that is an odd number of bytes (say 7 bytes), the compiler
will probably add 1 byte to pad it out to 8bytes (so the sizeof() function will return
the structure size as 8 bytes).
99.99% of the time this is ok. Is there a compiler driective I can insert in the structure definition if I want sizeof() to return the as_desgined number of bytes(in this particular example 7 bytes)?
 
Old 12-05-2006, 03:03 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
You can use __attribute((packed)). Why do you want to do this? There really aren't very many good reasons.
 
Old 12-05-2006, 03:38 PM   #3
guestgulkan
LQ Newbie
 
Registered: Nov 2005
Posts: 3

Original Poster
Rep: Reputation: 0
it's just that i'm messing about with usb - and at some points the devices returns data which is consecutive structures end to end - and they are not an even number of bytes each.
So my pointer arithmetic gets messed up if you get what I mean.
 
Old 12-05-2006, 03:57 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Okay, that's what I thought. Do you use bitfield structures? Not only does it do what you want, but it also isn't vulnerable to the various definitions of integer types.
 
  


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
string padding and replacement functions George2 Programming 4 11-19-2006 02:09 AM
How to find files within certain size boundaries? kornerr Linux - General 2 06-18-2006 12:14 AM
Are there proper partition block boundaries? GrumpyGnome Linux - Software 0 06-01-2004 03:34 PM
fdisk gives me warnign about cylinder boundaries.. zeppelin Linux - Hardware 2 10-19-2003 08:44 AM
Apache logging stops at 4k boundaries shilbert Linux - Software 2 06-26-2003 12:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:36 AM.

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