Re: Alignment - Structures and Other Things

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 04/14/2011 12:18 PM, Patrick Rutkowski wrote:
> 
> On Apr 14, 2011, at 5:36 AM, Andrew Haley wrote:
>>
>> I can't quite tell what you're asking.  C is designed so that you,
>> never have to know the alignment requirements: program in a
>> straightforward way and you'll be fine.  Is it that you don't know
>> what the rules are for writing C?  Or that you know the rules, but
>> for some special reason you want to violate them in non-portable
>> code?
> 
> Ah, yes, a well put question.
> 
> As to the first part, I think that I'm now starting to understand why
> alignment issues exist. If I understand correctly, it's because
> compilers often choose to use aligned CPU op codes (though I can't say
> I know exactly why aligned op codes run faster in hardware). And I
> think I know how these alignment issues are dealt with, which
> would be via malloc() aligning and struct padding.

Right.  Some reasons for slowness are obvious: if your bus is 32 bits
wide and you have an unaligned 32-bit fetch then you have to make two
accesses.  It's not so bad on the x86 chips most of us have in our
desktop machines, which have a bunch of special hardware to make
unaligned accesses fast in most cases.  However, sometimes an
unaligned fetch will straddle the boundary between two 64-byte cache
lines, which can hurt performance, so gcc tries hard to make this not
happen.

Andrew.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux