473,789 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unaligned pointers question

Up until now, I was under the impression that when one talks about data
alignment, what one means is at what address some data is stored. For
instance, if we write

unsigned int x ;

assuming that sizeof(unsigned int) is 4 then &x will evaluate to some
address the value of which will be a multiple of 4. The reasoning is
analogous for the other fundamental data types supported by a given
compiler.

However, I recently came across the following:

unsigned short * p = (unsigned short *) str ;

where str has been defined as unsigned char * elsewhere, and points to
some data. str can point to an address with an arbitrary value, as far as
divisibility is concerned.

unsigned short x ;
unsigned short * y = &x ;

*y = p[0] ;

In my naivete, I thought that this last line would result in an unaligned
access whenever the address p is not a multiple of 2 (assuming that
sizeof(unsigned short) is 2) but my little test code shows that sometimes
it does, sometimes it doesn't.

Can anybody enlighten me here? What is the foolproof criterion to
determine is some data is correctly aligned?
Nov 15 '05 #1
7 3731
On 2005-10-22, Steven Jones <sj****@sjones. org> wrote:
[...]
However, I recently came across the following:

unsigned short * p = (unsigned short *) str ;

where str has been defined as unsigned char * elsewhere, and points to
some data. str can point to an address with an arbitrary value, as far as
divisibility is concerned.

unsigned short x ;
unsigned short * y = &x ;

*y = p[0] ;

In my naivete, I thought that this last line would result in an unaligned
access whenever the address p is not a multiple of 2 (assuming that
sizeof(unsigned short) is 2) but my little test code shows that sometimes
it does, sometimes it doesn't.


I have to ask HOW you determined whether that line resulted in an
unaligned access or not. Your assertion that the divisibility of an
address indicates the alignment is correct in practice (though the C
standards do not guarantee that such a relationship between pointers and
integers exists.)

However, since unaligned access invokes undefined behavior in C, the
implementation is free to behave in any way it chooses upon encountering
such a construct, and that may be may be what you're seeing: Some
processors require correct alignment for all types and yield a bus error
for unaligned access, others do not (in particular, the x86 architecture
can perform misaligned data access in exchange for a performance
penalty), and still others may perform the access but silently yield
unwanted results. Some operating systems work around such processor
limitations by catching alignment faults and building your desired
results from combining two aligned reads/writes, and some compilers can
do the same thing in advance, such that the processor never gets to see
the bad access.

The fact that the access worked for you does not necessarily mean that
the address is really correctly aligned.

Nov 15 '05 #2
I hate following up to myself, but somebody's gotta do it ...

On 2005-10-22, Nils Weller <me@privacy.net > wrote:
results from combining two aligned reads/writes, and some compilers can
do the same thing in advance, such that the processor never gets to see
the bad access.


Actually compilers that try to prevent unaligned reads/writes in advance
tend to do so by reading/writing the data byte-wise rather than in units
of the particular type.

--
Nils R. Weller, Bremen (Germany)
My real email address is ``nils<at>gnuli nux<dot>nl''
.... but I'm not speaking for the Software Libre Foundation!
Nov 15 '05 #3
On Sat, 22 Oct 2005 21:31:44 +0000, Nils Weller wrote:
On 2005-10-22, Steven Jones <sj****@sjones. org> wrote:
[...]
However, I recently came across the following:

unsigned short * p = (unsigned short *) str ;

where str has been defined as unsigned char * elsewhere, and points to
some data. str can point to an address with an arbitrary value, as far
as divisibility is concerned.

unsigned short x ;
unsigned short * y = &x ;

*y = p[0] ;

In my naivete, I thought that this last line would result in an
unaligned access whenever the address p is not a multiple of 2 (assuming
that sizeof(unsigned short) is 2) but my little test code shows that
sometimes it does, sometimes it doesn't.
I have to ask HOW you determined whether that line resulted in an
unaligned access or not.


Well, when I run run the code a big fat warning is printed out that says
so. This is under Linux x86.
Your assertion that the divisibility of an address indicates the
alignment is correct in practice (though the C standards do not
guarantee that such a relationship between pointers and integers
exists.) However, since unaligned access invokes undefined behavior in C, the
implementation is free to behave in any way it chooses upon encountering
such a construct, and that may be may be what you're seeing: Some
processors require correct alignment for all types and yield a bus error
for unaligned access, others do not (in particular, the x86 architecture
can perform misaligned data access in exchange for a performance
penalty), and still others may perform the access but silently yield
unwanted results. Some operating systems work around such processor
limitations by catching alignment faults and building your desired
results from combining two aligned reads/writes, and some compilers can
do the same thing in advance, such that the processor never gets to see
the bad access.

The fact that the access worked for you does not necessarily mean that
the address is really correctly aligned.


Well, this is a nice explanation, but it does not really address the
issue I raised: Is the divisibility criterion I mentioned the right one
for determining data alignment, or does this vary so dramatically from
processor to processor that such criterion is not generally valid?

I am aware that this is not directly related to the C standard, but it is
a question that surely arises whenever one writes C code on any platform,
right?
Nov 15 '05 #4
On 2005-10-23, Steven Jones <sj****@sjones. org> wrote:
On Sat, 22 Oct 2005 21:31:44 +0000, Nils Weller wrote:
On 2005-10-22, Steven Jones <sj****@sjones. org> wrote:
[...]

I have to ask HOW you determined whether that line resulted in an
unaligned access or not.


Well, when I run run the code a big fat warning is printed out that says
so. This is under Linux x86.


Interesting, so you're running a Linux with x86 alignment exceptions
turned on. The x86 architecture, as I mentioned in my previous message,
can correctly handle unaligned memory access. However, it is also
capable of causing an exception upon encountering such an access, just
like those architectures that always respond with bus errors to
unaligned access. There's a bit in an x86 control register that can be
set to select the desired behavior. Most x86 systems do not make use of
this feature, so misaligned memory access will work on them without any
indication of a problem except for the increased access time.
Your assertion that the divisibility of an address indicates the
alignment is correct in practice (though the C standards do not
guarantee that such a relationship between pointers and integers
exists.)

[...]

Well, this is a nice explanation, but it does not really address the
issue I raised: Is the divisibility criterion I mentioned the right one
for determining data alignment, or does this vary so dramatically from
processor to processor that such criterion is not generally valid?


The very first paragraph of the explanation addresses the issue you
raised: Yes, the divisibility criterion you mentioned is the right one
for determining data alignment, in the real world (again, the C standard
does not say this.)

--
Nils R. Weller, Bremen (Germany)
My real email address is ``nils<at>gnuli nux<dot>nl''
.... but I'm not speaking for the Software Libre Foundation!
Nov 15 '05 #5
On Sun, 23 Oct 2005 02:04:24 +0000, Nils Weller wrote:
The very first paragraph of the explanation addresses the issue you
raised: Yes, the divisibility criterion you mentioned is the right one for
determining data alignment, in the real world (again, the C standard does
not say this.)


All right. I guess that the way to understand what I am observing is,
for some cases in which unaligned data access is taking place, the
platform that I am using is not reacting in any immediately visible way,
whereas in some others (for whatever reason) it does.
Nov 15 '05 #6
"Steven Jones" <sj****@sjones. org> wrote

All right. I guess that the way to understand what I am observing is,
for some cases in which unaligned data access is taking place, the
platform that I am using is not reacting in any immediately visible way,
whereas in some others (for whatever reason) it does.

Not all processors require data to be aligned.
Others slow down when they hit unaligned data, but give correct output.
Others require strict alignment.

Therefore casting a char * to an unsigned short is asking for trouble.
Normally there should be no reason to do this, but sometimes you do need to
play games with memory, in which case flag it as a portability hazard.
Nov 15 '05 #7
On Sun, 23 Oct 2005 01:22:33 GMT, Steven Jones <sj****@sjones. org>
wrote in comp.lang.c:
On Sat, 22 Oct 2005 21:31:44 +0000, Nils Weller wrote:
On 2005-10-22, Steven Jones <sj****@sjones. org> wrote:
[...]
However, I recently came across the following:

unsigned short * p = (unsigned short *) str ;

where str has been defined as unsigned char * elsewhere, and points to
some data. str can point to an address with an arbitrary value, as far
as divisibility is concerned.

unsigned short x ;
unsigned short * y = &x ;

*y = p[0] ;

In my naivete, I thought that this last line would result in an
unaligned access whenever the address p is not a multiple of 2 (assuming
that sizeof(unsigned short) is 2) but my little test code shows that
sometimes it does, sometimes it doesn't.


I have to ask HOW you determined whether that line resulted in an
unaligned access or not.


Well, when I run run the code a big fat warning is printed out that says
so. This is under Linux x86.
Your assertion that the divisibility of an address indicates the
alignment is correct in practice (though the C standards do not
guarantee that such a relationship between pointers and integers
exists.)

However, since unaligned access invokes undefined behavior in C, the
implementation is free to behave in any way it chooses upon encountering
such a construct, and that may be may be what you're seeing: Some
processors require correct alignment for all types and yield a bus error
for unaligned access, others do not (in particular, the x86 architecture
can perform misaligned data access in exchange for a performance
penalty), and still others may perform the access but silently yield
unwanted results. Some operating systems work around such processor
limitations by catching alignment faults and building your desired
results from combining two aligned reads/writes, and some compilers can
do the same thing in advance, such that the processor never gets to see
the bad access.

The fact that the access worked for you does not necessarily mean that
the address is really correctly aligned.


Well, this is a nice explanation, but it does not really address the
issue I raised: Is the divisibility criterion I mentioned the right one
for determining data alignment, or does this vary so dramatically from
processor to processor that such criterion is not generally valid?

I am aware that this is not directly related to the C standard, but it is
a question that surely arises whenever one writes C code on any platform,
right?


No, I would say that you are wrong. There is never any need to write
C code that accesses an lvalue via a pointer with incorrect alignment.

Most such code is written by those who decide, generally without
evidence or testing, that doing things properly is "too slow".

Almost all such decisions are based on incorrect assumptions
(premature optimization and all that) that would turn out to be just
plain wrong if actually measured or tested.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #8

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

Similar topics

46
2271
by: TTroy | last post by:
Hi, I'm just wondering why people/books/experts say "the function returns a pointer to.." or "we have to send scanf a pointer to.." instead of "the function returns the address of.." or "we have to send scanf the address of.." Isn't the lvalue called a POINTER TO and the (r)value called the ADDRESS OF?
18
10077
by: Pavel A. | last post by:
Hello, What is equivalent of __unaligned attribute in GNU C? I've searched for some time and it seems that it just does not exist... /* For HP and new Microsoft compilers it works this way: long_pointer = (some unaligned address) long_value = (* (__unaligned long *) long_pointer );
7
2355
by: fox | last post by:
Maybe this is not the best group to ask this question, but I don't know a better one. I'm looking for a *portable* program in C (I mean source code) to detect whether unaligned word access is: a. handled by the main processor (e.g. x86) b. not supported (e.g. Sparc running Solaris) c. emulated in software (e.g. Alpha running Linux) By "unaligned word access" I mean access to a 16-bit word
5
13929
by: Steven Woody | last post by:
hi, i get a struct as below, typedef struct { uint16_t id; long offset; } foo_t;
1
1637
by: Tim Lewis | last post by:
Using Visual Studio 2005 C++ compiler, I am getting a MOVDQA(in x64 mode) generated on an unaligned stack address. I actually have a debug screen shot showing RSP being set correctly at the start of the function. Here is the code: ; RSP = FFFB00 here, all numbers are in hex mov qword ptr , r8 mov qword ptr , rdx mov qword ptr , rcx
0
9499
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
10177
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
10121
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
9969
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...
1
7519
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
6750
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
5539
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4076
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
3677
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.