473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

P142 K&R

mdh
The 3rd paragraph says: "Alignment requirements can generally be
satisfied easily, at the cost of some wasted space, by ensuring that
the allocator always return a poiner that meets all ( italicized)
alignment restrictions"

I have looked at the threads about alignment in this group, and most
**assume** that the writer/replies understand fully what alignment
entails. There are some quite good explanations on the web, but none
really capture for me, at least, the essence of what alignment does,
and what can go wrong if alignment is not adhered to.

So, could anyone very very briefly explain the essence of alignment
and why the C programmer should worry about it and why it seems to
come up in structs and pointers..(if this last fact is indeed
true). :-).

Thanks as usual.
Aug 26 '08 #1
5 1536
mdh <md**@comcast.netwrites:
The 3rd paragraph says: "Alignment requirements can generally be
satisfied easily, at the cost of some wasted space, by ensuring that
the allocator always return a poiner that meets all ( italicized)
alignment restrictions"

I have looked at the threads about alignment in this group, and most
**assume** that the writer/replies understand fully what alignment
entails. There are some quite good explanations on the web, but none
really capture for me, at least, the essence of what alignment does,
and what can go wrong if alignment is not adhered to.

So, could anyone very very briefly explain the essence of alignment
and why the C programmer should worry about it and why it seems to
come up in structs and pointers..(if this last fact is indeed
true). :-).
The C99 standard actually has a definition of the word "alignment":

alignment

requirement that objects of a particular type be located on
storage boundaries with addresses that are particular multiples of
a byte address

Though it doesn't say what it means for an address to be a "multiple"
of something.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 26 '08 #2
mdh
On Aug 25, 5:58*pm, Keith Thompson <ks...@mib.orgwrote:
mdh <m...@comcast.netwrites:
The 3rd paragraph says: "Alignment requirements can generally be
satisfied easily, at the cost of some wasted space, by ensuring that
the allocator always return a poiner that meets all ( italicized)
alignment restrictions"
I have looked at the threads about alignment in this group, and most
**assume** that the writer/replies understand fully what alignment
entails. There are some quite good explanations on the web, but none
really capture for me, at least, the essence of what alignment does,
and what can go wrong if alignment is not adhered to.
So, could anyone very very briefly explain the essence of alignment
and why the C programmer should worry about it and why it seems to
come up in structs and pointers..(if this last fact is indeed
true). :-).

The C99 standard actually has a definition of the word "alignment":

* * alignment

* * requirement that objects of a particular type be located on
* * storage boundaries with addresses that are particular multiples of
* * a byte address

Though it doesn't say what it means for an address to be a "multiple"
of something.

--
Keith Thompson (The_Other_Keith) ks...@mib.org *<http://www.ghoti.net/~kst>
Nokia
"We must do something. *This is something. *Therefore, we must do this."
* * -- Antony Jay and Jonathan Lynn, "Yes Minister"
Thanks Keith...for once ( :-) ) I will accept it is something under
the hood I will deal with if I ever need that level of programming
skill.I do have a vague general sense for it...thanks to these
replies and wikipedia!!
Aug 26 '08 #3
Tim Prince wrote:
mdh wrote:
>The 3rd paragraph says: "Alignment requirements can generally be
satisfied easily, at the cost of some wasted space, by ensuring that
the allocator always return a poiner that meets all ( italicized)
alignment restrictions"

I have looked at the threads about alignment in this group, and most
**assume** that the writer/replies understand fully what alignment
entails. There are some quite good explanations on the web, but none
really capture for me, at least, the essence of what alignment does,
and what can go wrong if alignment is not adhered to.

So, could anyone very very briefly explain the essence of alignment
and why the C programmer should worry about it and why it seems to
come up in structs and pointers..(if this last fact is indeed
true). :-).

Thanks as usual.
No, this is not a brief subject. I'll try to compromise.
Most platforms have either a performance penalty or throw an address
fault upon misaligned access. Aligned addresses are those which are a
multiple of the size of the object (16 bytes for 128-bit parallel
objects on several platforms).
We know (most of us, anyway) what is meant, but what is
said is wrong. For example, this explanation implies that
a `char array[137];', since it is 137 bytes in size, must be
aligned on an address divisible by 137.

"Alignment requirements" are C's recognition of, or perhaps
concession to, some realities of the ways computers are built.
Most machines view memory as a big array of N bytes, with numeric
addresses running from 0 through N-1. A size-1 object like a
char can reside at any of these addresses, and the machine can
fetch, inspect, and store the char value at any of them.

But when it comes to multi-byte objects, machines are often
less flexible. For example, on a machine whose ints are four
bytes long, it may turn out that an int value in memory can only
be fetched and stored from an address divisible by four. That is,
the instructions that move ints to and from memory might view that
memory not as an array of bytes numbered 0 through N-1, but as an
array of ints numbered 0 through N/4-1. If so, then the first few
ints are the 0th, 1st, 2nd, 3rd, which correspond to starting byte
addresses of 0, 4, 8, 12. That is, the machine's view of memory
as an array of four-byte ints implies that an int can only start
on byte address divisible by four.

Why this artificial view? Because it can streamline the
hardware. For example, consider a machine whose memory management
unit naturally divides memory into "pages" of, say, 8KB, each with
its own access protections and other attributes. When the CPU tries
to fetch something from or store something to this memory, it must
check that the memory is mapped and grants the appropriate access
permissions. If a four-byte int necessarily starts at an address
divisible by four, it's easy to see that any such int lies entirely
in one memory page, so the CPU need only check the accessibility of
one of its bytes: the other three will necessarily be the same. But
if an int could start just anywhere, it could start one byte before
the end of a page with the other three protruding into the following
page: two access tests take more time and/or hardware than one, so
CPU designers prefer to avoid making them.

Getting back to Tim Prince's mis-statement: If we view memory
as an indexed array of bytes, we can talk about the divisibility
of a particular byte's index by this or that number. The alignment
requirement for a type T is a number Tn such that a T instance can
start at any byte whose index is divisible by Tn. By considering
the way arrays work in C, we can see that Tn must be a divisor of
sizeof(T), not a multiple thereof as Tim suggests. Thus, if an
int is four bytes long, one machine might require that it start
on a byte address divisible by 4, another might permit any even
address, and yet another might require only divisibility by 1:
any address at all will do. But no machine will require that a
four-byte int start on an address divisible by 3, or 6, or 8.
Similarly, a twelve-byte long double might require 1-, 2-, 3-, 4-,
6-, or 12-byte alignment, but will never need alignment on a byte
address that is a multiple of 7 or 24.

Finally, a word about the "necessity" of obeying alignment
requirements. When an instruction that expects Tn-byte alignment
encounters an operand that is not properly aligned, different
machines respond in different ways. The C Standard does not
prescribe any particular behavior, so this list of outcomes that
I have actually encountered may not be exhaustive:

1) The hardware simply pretends that the operand is aligned
and accesses a properly-aligned address near the one
actually specified. For example, a four-byte access to
address 0x87654321 might behave as if 0x87654320 had
been specified instead. This behavior is very fast, but
usually unwelcome.

2) The hardware detects the misalignment and generates some
kind of trap or fault, which the operating environment
then turns into a signal (or something like it) that
usually terminates the program abnormally. This behavior
is almost always unwelcome.

3) The hardware detects the misalignment and generates a trap
as above, but software in the trap handler figures out what
was attempted and then emulates it, using multiple memory
accesses along with shifting and masking to get at the
desired bytes. This usually produces a slowdown of between
a hundred- to a thousand-fold; that is, your 3GHz machine
runs at an effective speed of 3-30MHz.

4) The hardware detects the misalignment but does a hardware-
assisted fixup, in the style of (3) but with much greater
speed. This usually produces a slowdown of two- to three-
fold; your 3GHz machine runs at 1-1.5GHz.

.... and, as I mentioned, the C Standard does not limit the
outcome to any of these; as far as the Standard is concerned,
accessing a misaligned object may cause demons to fly out of
your misaligned nose.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 26 '08 #4
On Aug 25, 9:01*pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
* * *3) The hardware detects the misalignment and generates a trap
* * * * as above, but software in the trap handler figures out what
* * * * was attempted and then emulates it, using multiple memory
* * * * accesses along with shifting and masking to get at the
* * * * desired bytes. *This usually produces a slowdown of between
* * * * a hundred- to a thousand-fold; that is, your 3GHz machine
* * * * runs at an effective speed of 3-30MHz.

* * *4) The hardware detects the misalignment but does a hardware-
* * * * assisted fixup, in the style of (3) but with much greater
* * * * speed. *This usually produces a slowdown of two- to three-
* * * * fold; your 3GHz machine runs at 1-1.5GHz.

An important case to remember exists between your (3) and (4). On
hardware like PPC or IPF, where many/most misaligned accesses can be
resolved by the hardware with a moderate speed penalty, but others,
usually involving complex cases like crossing page boundaries, are
still punted back to a software trap to be fixed up (where the
misaligned access incurs the severe performance penalty).
Aug 26 '08 #5
mdh
On Aug 25, 7:01*pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
.
>
* * *"Alignment requirements" are C's recognition of, or perhaps
concession to, some realities of the ways computers are built.
Eric...thank you so much for that very extensive explanation. It is
now copied and pasted in my C "keep" files...hopefully totally
aligned! :-)
Aug 26 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character...
1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
0
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a...
4
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: ...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
11
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.