473,609 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3221
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******@engin eer.com> wrote in message
news:2d******** *************** ***@posting.goo gle.com...
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_w ord) 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.ne t> 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**********@CS i.com (Brian[dot]Inglis{at}Syste maticSW[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******@spamc op.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.ne t (Thomas Pornin) wrote:
According to Kenneth Brody <ke******@spamc op.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**********@CS i.com (Brian[dot]Inglis{at}Syste maticSW[dot]ab[dot]ca)
fake address use address above to reply
Nov 14 '05 #10

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

Similar topics

35
2877
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 */ memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/ According to my knowledge in the first line
20
2083
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 udpiphdr *ui; struct ip *ip; ip = (struct ip *) buf;
16
2286
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 subject pointer does not refer to an object suitably aligned in storage. It is guaranteed that a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and back again without change;...
10
353
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 change. My question is that, is it legal to dereference the intermediate pointer (the pointer with less strict alignment that we get after conversion) ? Does this intermediate pointer also point to the same memory location as the pointer (with more...
13
2983
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 sizeof(T). (Proof: In `T array;' both `array' and `array' must be correctly aligned.) Since `sizeof(unsigned char)' is divisible only by one and since one is a divisor of every type's size, every type is suitably aligned for `unsigned char'."
17
2712
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 compiler I use for the Samsung 16 bit Smartcard chips and I want to know if it is compliant with the ANSI standard or if it violates it. Have a look at this super simple example, where the value of b is incorrect:
10
2195
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; char nodes; //will be list_node1,list_node2... };
14
4032
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 right thing to do?
11
2291
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 aligned on specific memory boundaries. Moreover, in many situations the cryptographic code does not itself have control over the memory alignment of its parameters so the best it can do is to detect if these aligmments are correct and act...
0
8534
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
8509
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
8188
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
8374
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6969
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
6034
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...
0
5502
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4002
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1630
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.