LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 04-03-2007, 10:02 PM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
memory alignment of a struct


Hello everyone,


I heard when define a struct, it is important to make the size of the struct memory aligned.

For example,

Code:
struct foo
{
    char p;
    char[3] align;
};
Code:
struct goo
{
    char p;
};
struct foo is better than struct goo, even if we only use member p.

Allocate 3 bytes to align with memory. But I do not know why make memory aligned is better. Any reasons? (for example, prevent memory from running out?)


thanks in advance,
George
 
Old 04-03-2007, 11:07 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
struct goo is better than struct foo. Leave padding and alignment issues to the compiler, it knows better than you what to do.

Anyway, the reason why aligned memory is sometimes better is because some processors are optimized for aligned access of words (it's the common case), and faster access for aligned bytes comes naturally from that.
 
Old 04-03-2007, 11:09 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
In days gone by that was important, but I'm not so sure now.

The reason for such an approach in the structures was (and still is) that numeric calculations were performed faster if they started on a word boundary, hence padding was occasionally required. This goes back to my days of Fortran, Pascal and PL/1 where structures would declare individual bits, and so it was possible for an integer to be declared at a very inconvenient location.

Looking at C the language does the padding automatically for you. If you define a structure with a char, an int, a char and an int, use sizeof to see the size of that structure. Now define a structure with two intsfollowed by two chars and use sizeof again. The first structure will add padding so that the ints are on the proper boundaries, whilst the second doesn't need the padding.

Last edited by graemef; 04-03-2007 at 11:10 PM.
 
Old 04-04-2007, 10:48 AM   #4
KenJennings
LQ Newbie
 
Registered: Mar 2005
Location: FL, USA
Distribution: SuSE
Posts: 28

Rep: Reputation: 15
Internal padding in the structure is platform dependent. Under ordinary circumstances you should not worry about alignment, since the compiler will handle it automatically.

However, if you have lot of elements in a structure definition and have the urge to save memory, then grouping like types together will usually reduce the amount of padding that occurs. (Do some diagnostics and printf the starting addresses of items in the structure to see if there are any improvements.)
 
Old 04-04-2007, 07:45 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
There are various obscure reasons for it. The memory pipelines, internal and external caches and so-forth in a modern CPU are "wide," so they actually transfer a chunk of memory to or from the CPU each time. Compiler-writers are aware of the capabilities and the preferences of various types of hardware and they do a lot of things for you.

About the only place where you might need to give such things "conscious thought" is when you are matching your code to the data-format of some external data source or file. In that case, you might need to get very specific about exactly which byte goes where, whether they're "big-endian" or "little-endian," and more.
 
  


Reply



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
struct member alignment wmoti Programming 2 10-10-2005 05:24 AM
seg. fault when allocating memory via a pointer inside a struct elmafiacs Programming 4 02-20-2005 07:26 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM

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

All times are GMT -5. The time now is 11:09 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