473,785 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1561
mdh <md**@comcast.n etwrites:
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_Keit h) 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.orgw rote:
mdh <m...@comcast.n etwrites:
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_Keit h) 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.invalidwrot e:
* * *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.invalidwrot e:
.
>
* * *"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...hopeful ly totally
aligned! :-)
Aug 26 '08 #6

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

Similar topics

9
8580
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 reference (i.e.  ) and it seems to be converting the character to its UNICODE representation. Take this source XML document: <?xml version="1.0" encoding="UTF-8"?>
1
11444
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 href="mailto:DrTebi@yahoo.com">DrTebi</a> This would work like a regular mailto link in any browser, but wouldn't be visible to spiders if they don't have a function to decode it.
0
2423
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 href="http://blah.com/?test=test&amp;amp;test2=test2">Test2&amp;amp;</a> This results in the following HTML Code:
4
3029
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: /mypage.asp?value1=1&amp;value2=4&amp; ... which of course means nothing to asp.
4
3229
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 that I could externalize my JavaScript, but that will not be practical throughout this application. Is there any way to get around this issue? Xalan processor. Stripped down stylesheet below along with XHTML output. <?xml version='1.0'?>...
8
2818
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);" TextBox2.Attributes.Add("onKeyPress", jscode) You will notice that jscode contains the JavaScript Logical And operator (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
11
6444
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 and
14
5935
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 because of this, so I hope some of you gurus can enlighten me with this :) In what circumstances can the "&amp;" in the source code be involuntary changed to "&" by a browser when or other software, when editing and uploading the file to the web...
12
10118
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) ..... // xsl contents abc.aspx?p1=v1&amp;p2=<xsl:value-of select="$v2" />
7
4628
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 BeautifulSoup to clean those up? There are various parsing options related to "&" handling, but none of them seem to do quite the right thing. If I write the BeautifulSoup parse tree back out with "prettify", the loose "&" is still in there. So...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10324
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.