LinuxQuestions.org
Register a domain and help support LQ
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 01-25-2010, 08:53 AM   #1
aryan1
Member
 
Registered: Jul 2009
Posts: 50

Rep: Reputation: 16
Question how to disable structure alignment using gcc compiler options ?


Hi All,

How can I disable structure alignment feature of gcc using command-line options ?

I recently migrated to 64-bit OS, and doubt that I might be experiencing a structure alignment problem due to the new 64-bit architecture.

I checked the sizes of the same C-style struct in both x86 and x86_64, and found out that they appear to be different by 20 bytes.

I am not sure if this is due to structure alignment or the differences in data type lengths between two platforms.

Hence, I will first disable the structure alignment feature, and then check the struct sizes again.

Thanks.
 
Old 01-25-2010, 11:02 AM   #2
Xyro
LQ Newbie
 
Registered: Aug 2009
Location: Canada
Distribution: Ubuntu 9.04
Posts: 22

Rep: Reputation: 19
Are you looking for:

#pragma pack(push, 1)

This will set the compiler to pack structures in 1 byte chunks (no fillers)

#pragma pack(pop)

will get you back to default.


Edit -- Sorry, I replied to quickly. Yes, you are correct. The size of individual datatypes change. long in 32bit = 4bytes, 8 bytes in 64bit OS from my experience. If you cannot change the structure to use variables with set sizes (uint32_t for ex.), then you need to d/l the 32-bit libs (Google is your friend here, as I cant recall the names of the libs req'd, buildessentials is one--*I think*) and compile with the -m32 flag to get 32bit object code (and hence datatypes).

Edit again: bad name for 32 bit int datatype...

Last edited by Xyro; 01-26-2010 at 10:22 AM.
 
Old 01-25-2010, 01:26 PM   #3
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Hey,

it is basically both, the data type sizes (long equals 4bytes on 32bit vs 8bytes on 64bit) change AND the alignment requirements change as well. 4-byte data types are aligned on a 4-byte boundary, while 8-byte data types are aligned on an 8-byte boundary. So, I doubt that packing would help here. By the way, just for completeness and to avoid the #pragma directives of the compiler, you could do the following to specify the "pack" attribute per structure definition:

[CODE]
struct a
{
...
} __attribute__((packed));

But note, the __attribute__ syntax is a GNU extension which you wouldn't find on any other compiler.


Lastly, why do you think your application will crash due to the port to 64-bit architecture? How do you fill your structures? If you would like to, you could post your structure and extracts of your code which you think will crash ...

- Andi -
 
Old 01-26-2010, 09:21 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,368
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
If your structure represents a hardware layout, then you can force the sizes of components of the structure using the predefined types from stdint.h (such as uint16_t etc), or by using the ':n' notation, eg
Code:
unsigned int foo:16;
Both the packing and alignment can be set using the packed and aligned attributes as demonstrated in the above posts.

However, you should be careful of such controlled structures; they are seldom portable (such as when moving between little endian to big endian architectures), and often represent a failure to serialize data. The best reason for packing structures is to conserve space (usually at the expense of speed), not to force a consistent byte format.

Last edited by neonsignal; 01-26-2010 at 09:43 AM.
 
  


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
GNU Make and GNU GCC DEBUG vs RELEASE compiler options aryan1 Programming 1 01-12-2010 01:29 PM
gcc pre-compiler options !!!!! webquinty Programming 2 09-30-2008 04:57 PM
Graph data structure generation options and parametrization hamtavs Programming 1 04-18-2008 04:44 PM
wget options for directory structure davimint Linux - Server 3 05-18-2007 11:24 PM
GCC compiler options???!! Umanga Linux - Newbie 1 10-08-2004 04:16 AM


All times are GMT -5. The time now is 03:45 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration