473,382 Members | 1,441 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,382 software developers and data experts.

pointer alignment

for any pointer to T, does a pointer to T have different or can have
different alignment requirement than a pointer to pointer to T? if so,
where is the exact wording in the standard that would suggest so?
Nov 14 '05 #1
10 3181
j0mbolar wrote:

for any pointer to T, does a pointer to T have different or can have
different alignment requirement than a pointer to pointer to T? if so,
where is the exact wording in the standard that would suggest so?


I can't quote chapter and verse, but I can tell you that on the platforms
I currently use:

A pointer to "char" has no alignment restrictions.
A pointer to a 16-bit value must be aligned on a 16-bit boundary.
(ie: the address must be even.)
Pointers are 32 bits, and a pointer-to-pointer must be aligned on
a 32-bit boundary. (ie: the address must be a multiple of 4.)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Nov 14 '05 #2
"j0mbolar" <j0******@engineer.com> wrote in message
news:2d**************************@posting.google.c om...
for any pointer to T, does a pointer to T have different or can have
different alignment requirement than a pointer to pointer to T? if so,
where is the exact wording in the standard that would suggest so?


6.2.5#26:

A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type. Similarly, pointers to
qualified or unqualified versions of compatible types shall have the same
representation and alignment requirements. All pointers to structure types
shall have the same representation and alignment requirements as each other.
All pointers to union types shall have the same representation and
alignment requirements as each other. Pointers to other types need not have
the same representation or alignment requirements.
Nov 14 '05 #3
Kenneth Brody wrote:

j0mbolar wrote:

for any pointer to T, does a pointer to T have different or can have
different alignment requirement than a pointer to pointer to T? if so,
where is the exact wording in the standard that would suggest so?
I can't quote chapter and verse, but I can tell you that on the platforms
I currently use:

A pointer to "char" has no alignment restrictions.


A char has no alignment restrictions. A pointer to char may.
A pointer to a 16-bit value must be aligned on a 16-bit boundary.
(ie: the address must be even.)
Pointers are 32 bits, and a pointer-to-pointer must be aligned on
a 32-bit boundary. (ie: the address must be a multiple of 4.)


This is not required, either. In fact, PC-based computers don't need
(and at least some compilers don't have) alignment requirements for
pointers and 16-bit ints.

Thad
Nov 14 '05 #4
j0mbolar wrote:
for any pointer to T, does a pointer to T have different or can have
different alignment requirement than a pointer to pointer to T? if so,
where is the exact wording in the standard that would suggest so?


The standard doesn't "suggest" how the implementor
should allocate storage to objects of different
types, although it does impose some constraints.
Because it doesn't say otherwise, an implementor
is free to use different sizes for different types
of object pointer.

For example, pointer to byte often employs a
multi-word representation (word_address,
offset_within_word) whereas pointer to a word-
aligned object uses a single word )word_address).
Thus, char* would be fatter than char**.

Nov 14 '05 #5
Thad Smith wrote:
A char has no alignment restrictions. A pointer to char may.
I think he meant, the pointer value.
This is not required, either. In fact, PC-based computers don't need
(and at least some compilers don't have) alignment requirements for
pointers and 16-bit ints.


Not all computers are PCs, and even so, there can be
a performance penalty in using such unaligned access,
so a C implementation might very well adhere to such
alignment, at which point it might gain some further
benefit from being able to rely on that property.

The VAX was an early example where unaligned access
of multibyte objects was allowed, but inordinately
expensive (at least on some models).

Nov 14 '05 #6
On Thu, 26 Aug 2004 19:22:47 -0400 in comp.std.c, "Douglas A. Gwyn"
<DA****@null.net> wrote:
Thad Smith wrote:
A char has no alignment restrictions. A pointer to char may.


I think he meant, the pointer value.
This is not required, either. In fact, PC-based computers don't need
(and at least some compilers don't have) alignment requirements for
pointers and 16-bit ints.


Not all computers are PCs, and even so, there can be
a performance penalty in using such unaligned access,
so a C implementation might very well adhere to such
alignment, at which point it might gain some further
benefit from being able to rely on that property.


Indeed, GNU C for 386&c. seems to generate a lot of alignment opcodes.

--
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada

Br**********@CSi.com (Brian[dot]Inglis{at}SystematicSW[dot]ab[dot]ca)
fake address use address above to reply
Nov 14 '05 #7
Thad Smith wrote:

Kenneth Brody wrote:

j0mbolar wrote:

for any pointer to T, does a pointer to T have different or can have
different alignment requirement than a pointer to pointer to T? if so,
where is the exact wording in the standard that would suggest so?


I can't quote chapter and verse, but I can tell you that on the platforms
I currently use:

A pointer to "char" has no alignment restrictions.


A char has no alignment restrictions. A pointer to char may.


I guess it comes down to interpretation of the question. Yes, where
the char* is stored may have alignment restrictions, but the value of
the pointer does not.
A pointer to a 16-bit value must be aligned on a 16-bit boundary.
(ie: the address must be even.)
Pointers are 32 bits, and a pointer-to-pointer must be aligned on
a 32-bit boundary. (ie: the address must be a multiple of 4.)


This is not required, either. In fact, PC-based computers don't need
(and at least some compilers don't have) alignment requirements for
pointers and 16-bit ints.


True, "PC-based computers" can store anything at any valid address without
alignment restrictions. However, you do get a performance penalty for not
aligning things. The compilers I use on such systems default to using
alignment for such things, though you can turn this off.

Many other systems forbid non-aligned values and generate a fault if you
try to do so. Some of these systems will then, via software, handle the
mis-aligned access, allowing the program to run with a severe penalty hit.
Others will crash the program.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Nov 14 '05 #8
According to Kenneth Brody <ke******@spamcop.net>:
True, "PC-based computers" can store anything at any valid address without
alignment restrictions. However, you do get a performance penalty for not
aligning things. The compilers I use on such systems default to using
alignment for such things, though you can turn this off.


Actually, in "recent" Intel (and compatible) x86 processors (I think
the 80486 is the first to have this feature), there is a flag in the
eflags register which activates alignment checks. When this flag is set,
unaligned access trigger an exception, and subsequent process death
(unless the OS traps the exception and corrects things, which is not
customary in x86-based OS).

This flag is rarely set, mostly because the standard ABIs on 32-bit x86
(ELF for the Unix world, PE for Windows) specify only 32-bit alignment
for "double" values (which is fine for a 80386, but not for a modern
Pentium, which has 64-bit alignment requirements for "double" values).

If the flag is not set, the penalty hit for misaligned access is about
one clock cycle, which is huge for the most recent processors, which
may execute many opcodes during each clock cycle. There are also cache
issues, if the misaligned access spans accross a cache line boundary.
--Thomas Pornin
Nov 14 '05 #9
On Sun, 29 Aug 2004 08:46:15 +0000 (UTC) in comp.std.c,
po****@nerim.net (Thomas Pornin) wrote:
According to Kenneth Brody <ke******@spamcop.net>:
True, "PC-based computers" can store anything at any valid address without
alignment restrictions. However, you do get a performance penalty for not
aligning things. The compilers I use on such systems default to using
alignment for such things, though you can turn this off.


Actually, in "recent" Intel (and compatible) x86 processors (I think
the 80486 is the first to have this feature), there is a flag in the
eflags register which activates alignment checks. When this flag is set,
unaligned access trigger an exception, and subsequent process death
(unless the OS traps the exception and corrects things, which is not
customary in x86-based OS).

This flag is rarely set, mostly because the standard ABIs on 32-bit x86
(ELF for the Unix world, PE for Windows) specify only 32-bit alignment
for "double" values (which is fine for a 80386, but not for a modern
Pentium, which has 64-bit alignment requirements for "double" values).

If the flag is not set, the penalty hit for misaligned access is about
one clock cycle, which is huge for the most recent processors, which
may execute many opcodes during each clock cycle. There are also cache
issues, if the misaligned access spans accross a cache line boundary.


This approach makes a lot of sense, as a translation unit can then be
compiled with architectural alignment internally, ABI alignment for
external interfaces, or with no alignment in packed structures, or
compiled for a slight variant of the architecture on which it runs,
suffering only a speed penalty when alignment restrictions are not
strictly obeyed, rather than failure due to an exception.

--
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada

Br**********@CSi.com (Brian[dot]Inglis{at}SystematicSW[dot]ab[dot]ca)
fake address use address above to reply
Nov 14 '05 #10
According to Brian Inglis <Br**********@SystematicSW.ab.ca>:
This approach makes a lot of sense, as a translation unit can then be
compiled with architectural alignment internally, ABI alignment for
external interfaces, or with no alignment in packed structures, or
compiled for a slight variant of the architecture on which it runs,
suffering only a speed penalty when alignment restrictions are not
strictly obeyed, rather than failure due to an exception.


I prefer the Alpha way. Data access on the Alpha should be aligned;
unaligned access triggers an exception (there are also opcodes for
unaligned access, which do not fail, but which are a bit slower, with
somehow the same speed penalty then a Pentium doing an unaligned
access). Traditionaly, Unix-like OS on the Alpha trap the exception, and
perform the access by software, which implies a huge penalty (thousands
of clock cycles) but no process failure. The offending access is logged.

This way, one can use legacy code which performs unaligned access, and
the OS helps in tracking down such misbehaved programs. When I got an
Alpha as a desktop machine in 1998, I installed Linux on it, and at
that time many programs were faulty with regards to alignment (they
were designed for a 32-bit world, not a 64-bit world, and programmers
did quite evil things with their values); the most prominent villain
was the XFree server. I was quite thankful to the OS for the automatic
correction of misaligned access because XFree was quite necessary to my
daily work.
Of course, the x86 platform has a long history of allowing misaligned
access, and too much code which uses such accesses; hence, the cpu
vendors (Intel, AMD,...) have no choice but to add the necessary logic
to handle such accesses with a minimal speed penalty. From their point
of view, they would gladly do without unaligned accesses, but legacy
code is stronger.
--Thomas Pornin
Nov 14 '05 #11

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

Similar topics

35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
20
by: j0mbolar | last post by:
I was reading page 720 of unix network programming, volume one, second edition. In this udp_write function he does the following: void udp_write(char *buf, <everything else omitted) struct...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
10
by: junky_fellow | last post by:
K&R say that, It is guaranteed that 1) a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and 2) back again without...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
10
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
14
by: shaanxxx | last post by:
int main() { int *i = (int *) &i; } Above statement is a valid statement as per standard ? Since object is getting created and same time i am trying to store its address in itself, it is...
11
by: Brian Gladman | last post by:
A lot of low level cryptographic code and some hardware cryptographic accelerators either fail completely or perform very poorly if their input, output and/or key storage areas in memory are not...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.