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

Does UNALIGNED attribute exist in GNU C?

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 );

This causes the compiler to emit code that gets the value
in some slow but sure way, but avoiding exceptions
*/

No need to suggest workarounds please...

Regards
--PA
Nov 14 '05 #1
18 10022
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Pavel A." <pa*****@NOwritemeNO.com> writes:
What is equivalent of __unaligned attribute in GNU C?
I've searched for some time and it seems that it just does not exist...


It appears not to. That's probably because it should not be needed
unless your code is buggy. If your code is standard C, the compiler
should have aligned everything nicely for you. However, you didn't
post the code that creates the unaligned pointer.

Also:
- - Some CPUs allow unaligned access.
- - Some operating systems can transparently handle unaligned access on
systems that don't support it.
- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iD8DBQFCjOFQVcFcaSW/uEgRAu+aAKDDQqLP5PeBpB4RHt2Wn8bGN4lEXwCfQa4U
MDJleeQZG8u63b7TJZExNog=
=XMLL
-----END PGP SIGNATURE-----
Nov 14 '05 #2
[fu-t set]

in comp.lang.c i read:
What is equivalent of __unaligned attribute in GNU C?
I've searched for some time and it seems that it just does not exist...


correct -- there is nothing standard.

--
a signature
Nov 14 '05 #3
Thanks Roger for your reply.
I was looking how to implement functions like LoadUnalignedLong /
StoreUnalignedLong
(the need for these is obvious... if you get unaligned structure from file
or network etc).
So will do it anyway else.

--PA

"Roger Leigh" <${*******@invalid.whinlatter.ukfsn.org.invalid> wrote in
message news:87************@hardknott.home.whinlatter.ukfs n.org...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Pavel A." <pa*****@NOwritemeNO.com> writes:
What is equivalent of __unaligned attribute in GNU C?
I've searched for some time and it seems that it just does not exist...
It appears not to. That's probably because it should not be needed
unless your code is buggy. If your code is standard C, the compiler
should have aligned everything nicely for you. However, you didn't
post the code that creates the unaligned pointer.

Also:
- - Some CPUs allow unaligned access.
- - Some operating systems can transparently handle unaligned access on
systems that don't support it.
- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your

mail. -----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iD8DBQFCjOFQVcFcaSW/uEgRAu+aAKDDQqLP5PeBpB4RHt2Wn8bGN4lEXwCfQa4U
MDJleeQZG8u63b7TJZExNog=
=XMLL
-----END PGP SIGNATURE-----

Nov 14 '05 #4
"Pavel A." wrote:

I was looking how to implement functions like LoadUnalignedLong /
StoreUnalignedLong (the need for these is obvious... if you get
unaligned structure from file or network etc).
So will do it anyway else.


Please don't toppost. It is not acceptable in c.l.c. See sig
below.

There is no such need. How do you get an "unaligned structure"?
All you can receive from a network of file is some gestalt of
bytes, which should have a defined relationship to a field in a
structure. The details of a structure are not valid outside the
system that created it, and can be freely changed, even by a
compiler upgrade.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Nov 14 '05 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Pavel A." <pa*****@NOwritemeNO.com> writes:

[Please don't top-post]
I was looking how to implement functions like LoadUnalignedLong /
StoreUnalignedLong
(the need for these is obvious... if you get unaligned structure from file
or network etc).


I've never seen those non-standard functions. Due to the hideous
naming, I assume they come from MS Windows?

The need for them aside, you could always memcpy to a known aligned
memory location before reading/writing. It should be fairly cheap,
but still less than ideal. If your software is properly written, you
simply won't have alignment issues.
- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iD8DBQFCkMXUVcFcaSW/uEgRAq1GAJ9GYG9AuzlZcETFP5rGatDYmrDu7gCfX20d
TKSkV3oERlS9U9DF21grf60=
=BlzH
-----END PGP SIGNATURE-----
Nov 14 '05 #6
Roger Leigh <${*******@invalid.whinlatter.ukfsn.org.invalid> wrote:
"Pavel A." <pa*****@NOwritemeNO.com> writes:

[Please don't top-post]
I was looking how to implement functions like LoadUnalignedLong /
StoreUnalignedLong
(the need for these is obvious... if you get unaligned structure from file
or network etc).
I've never seen those non-standard functions. Due to the hideous
naming, I assume they come from MS Windows?


You'd say so, but AFAICT not even that.
The need for them aside, you could always memcpy to a known aligned
memory location before reading/writing. It should be fairly cheap,
but still less than ideal.


If you have a halfway decently defined network or file format, you won't
be reading directly into your objects if that's impossible. You'll be
manipulating the byte _values_, rather than their representations in
memory.

Richard
Nov 14 '05 #7
On Thu, 19 May 2005 19:56:17 +0100, Roger Leigh wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Pavel A." <pa*****@NOwritemeNO.com> writes:
What is equivalent of __unaligned attribute in GNU C?
I've searched for some time and it seems that it just does not exist...


It appears not to. That's probably because it should not be needed
unless your code is buggy.


I respectfully disagree: In networking applications, for performance
reasons (to avoid data copying) one must frequently deal with portions of
buffers that are not correctly aligned.
Nov 14 '05 #8
Thomas Carter wrote:
On Thu, 19 May 2005 19:56:17 +0100, Roger Leigh wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Pavel A." <pa*****@NOwritemeNO.com> writes:

What is equivalent of __unaligned attribute in GNU C?
I've searched for some time and it seems that it just does not exist...


It appears not to. That's probably because it should not be needed
unless your code is buggy.

I respectfully disagree: In networking applications, for performance
reasons (to avoid data copying) one must frequently deal with portions of
buffers that are not correctly aligned.

That depends on what you mean by unaligned. The C standards don't allow
the kinds of type punning which would place, for example, a long long
int at an odd short int address. Code which does so is non-portable.
Even Microsoft C, in the newer 64-bit versions, gets away from the
requirement to maximize support of 64-bit data located on any 32-bit
boundary, but this NG is about standard C.
If you mean, for example, a string of ints starting at any legal int
boundary, that's the default, and doesn't require any attribute or
pragma. gcc does support specification of higher alignments, such as
those which would allow the use of aligned pair load and store instructions.
Nov 14 '05 #9
On Sat, 18 Jun 2005 14:50:47 +0000, Thomas Carter wrote:
On Thu, 19 May 2005 19:56:17 +0100, Roger Leigh wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Pavel A." <pa*****@NOwritemeNO.com> writes:
What is equivalent of __unaligned attribute in GNU C?
I've searched for some time and it seems that it just does not exist...


It appears not to. That's probably because it should not be needed
unless your code is buggy.


I respectfully disagree: In networking applications, for performance
reasons (to avoid data copying) one must frequently deal with portions of
buffers that are not correctly aligned.


This is a generalisation which is the for most part wrong. Not totally
wrong but still mostly wrong. However a discussion of why is beyond the
scope of comp.lang.c (i.e. where I am reading this).

Lawrence
Nov 14 '05 #10
On Sun, 19 Jun 2005 13:36:52 +0100, Lawrence Kirby wrote:
On Sat, 18 Jun 2005 14:50:47 +0000, Thomas Carter wrote:
On Thu, 19 May 2005 19:56:17 +0100, Roger Leigh wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Pavel A." <pa*****@NOwritemeNO.com> writes:

What is equivalent of __unaligned attribute in GNU C?
I've searched for some time and it seems that it just does not exist...

It appears not to. That's probably because it should not be needed
unless your code is buggy.


I respectfully disagree: In networking applications, for performance
reasons (to avoid data copying) one must frequently deal with portions of
buffers that are not correctly aligned.


This is a generalisation which is the for most part wrong. Not totally
wrong but still mostly wrong.


typedef unsigned int UInt32 ;
UInt32 * x = (UInt32 *) malloc(10) ;
UInt32 * p = x + 1 ;

f(p) ;

f() is now dealing with an unaligned buffer. Lots of examples similar to
this can be found e.g. in the OpenSSL library.

That's what I meant.

Nov 14 '05 #11
Thomas Carter <T.******@nanobots.com> writes:
typedef unsigned int UInt32 ;
UInt32 * x = (UInt32 *) malloc(10) ;
UInt32 * p = x + 1 ;

f(p) ;

f() is now dealing with an unaligned buffer.


No, it isn't. Perhaps you need a refresher on how 'C' pointers work?

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Nov 14 '05 #12
Thomas Carter wrote:
typedef unsigned int UInt32 ;
UInt32 * x = (UInt32 *) malloc(10) ;
UInt32 * p = x + 1 ;

f(p) ;

f() is now dealing with an unaligned buffer.


No it isn't.

Go over the following carefully, testing it with your implementation.
Then go back to your elementary C text and learn how pointer arithmetic
works.

#include <stdio.h>
#include <stdlib.h>

#define MAGICNUMBER 10

int main(void)
{
unsigned int *x = malloc(MAGICNUMBER);
unsigned int *p = x + 1;
unsigned i;
if (!x) {
fprintf(stderr, "The allocation of x failed.\n"
"Bailing out ...\n");
exit(EXIT_FAILURE);
}
printf("The buffer x is of %u unsigned ints.\n"
"Each is properly aligned.\n"
"The addresses x[i] are:\n", MAGICNUMBER);
for (i = 0; i < MAGICNUMBER; i++)
printf(" &x[%u] = %p, x+%u = %p\n",
i, (void *) &x[i], i, (void *) (x + i));
printf("\nAnd p, initialized to point at x + 1, has the\n"
"same value as the correctly aligned &x[1] above:\n"
" p = %p\n", (void *) p);
free(x);
return 0;
}
[output on my implementation]
The buffer x is of 10 unsigned ints.
Each is properly aligned.
The addresses x[i] are:
&x[0] = 20d50, x+0 = 20d50
&x[1] = 20d54, x+1 = 20d54
&x[2] = 20d58, x+2 = 20d58
&x[3] = 20d5c, x+3 = 20d5c
&x[4] = 20d60, x+4 = 20d60
&x[5] = 20d64, x+5 = 20d64
&x[6] = 20d68, x+6 = 20d68
&x[7] = 20d6c, x+7 = 20d6c
&x[8] = 20d70, x+8 = 20d70
&x[9] = 20d74, x+9 = 20d74

And p, initialized to point at x + 1, has the
same value as the correctly aligned &x[1] above:
p = 20d54
Nov 14 '05 #13
Thomas Carter <T.******@nanobots.com> writes:
On Sun, 19 Jun 2005 13:36:52 +0100, Lawrence Kirby wrote:
On Sat, 18 Jun 2005 14:50:47 +0000, Thomas Carter wrote:
On Thu, 19 May 2005 19:56:17 +0100, Roger Leigh wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Pavel A." <pa*****@NOwritemeNO.com> writes:

> What is equivalent of __unaligned attribute in GNU C?
> I've searched for some time and it seems that it just does not exist...

It appears not to. That's probably because it should not be needed
unless your code is buggy.

I respectfully disagree: In networking applications, for performance
reasons (to avoid data copying) one must frequently deal with portions of
buffers that are not correctly aligned.


This is a generalisation which is the for most part wrong. Not totally
wrong but still mostly wrong.


typedef unsigned int UInt32 ;
UInt32 * x = (UInt32 *) malloc(10) ;
UInt32 * p = x + 1 ;

f(p) ;

f() is now dealing with an unaligned buffer. Lots of examples similar to


1) Wrong. p will point to x + sizeof(UInt32) .
2) If UInt32 means "unsigned int 32 bits long" there is no guarantee
that a plain "unsigned int" is 32 bits long. Actually, with my
compiler & platform of choice, it is 64 bits long.

--
Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
Dept. of Physics, Univ. of Padova, Italy ROT13: yb****@cq.vasa.vg
Nov 14 '05 #14
In article <BS*****************@newsread3.news.atl.earthlink. net>
Martin Ambuhl <ma*****@earthlink.net> wrote:
#define MAGICNUMBER 10
[the magic number came from elsewhere; all Martin Ambuhl did was
move it to a #define.]
unsigned int *x = malloc(MAGICNUMBER); .... if (!x) {
fprintf(stderr, "The allocation of x failed.\n"
"Bailing out ...\n");
exit(EXIT_FAILURE);
}
printf("The buffer x is of %u unsigned ints.\n"
"Each is properly aligned.\n"
"The addresses x[i] are:\n", MAGICNUMBER);


It is worth noting here that "x" now points to ten *bytes*, not
ten "unsigned int"s. If sizeof(unsigned int) is 2, this is five
"unsigned int"s; if sizeof(unsigned int) is 4, this is two with
two leftover bytes; and if sizeof(unsigned int) is 8, this is one
unsigned int, with two leftover bytes again. Only in the case when
sizeof(unsigned int) == 1 -- which is probably not supported by
GCC, although this does occur on some standalone implementations
-- is it ten unsigned ints.

Of course, it is true that sometimes one must deal with "misaligned"
memory, in places like network drivers (Ethernet headers are,
inconveniently, 14 bytes instead of 16). But there are ways to
deal with this without ever using a single line of nonstandard C.
Sometimes -- perhaps even "often" or "usually" -- the portability
gain of doing byte-at-a-time arithmetic to work with 16 and 32 bit
values outweighs the (usually tiny) performance gain one can obtain
using a compiler's "special features" for accessing unaligned value.
(The only way to find out for sure, of course, is to do both,
and measure.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #15
Chris Torek wrote:
It is worth noting here that "x" now points to ten *bytes*, not
ten "unsigned int"s.


Oh crap. Thank you.
Nov 14 '05 #16
On Sun, 19 Jun 2005 08:51:01 -0700, Paul Pluzhnikov wrote:
Thomas Carter <T.******@nanobots.com> writes:
typedef unsigned int UInt32 ;
UInt32 * x = (UInt32 *) malloc(10) ;
UInt32 * p = x + 1 ;

f(p) ;

f() is now dealing with an unaligned buffer.


No, it isn't. Perhaps you need a refresher on how 'C' pointers work?


I actually forgot the definition of f :-(

void f(UInt32 * p)
{
UInt64 * q = (UInt64 *) p ;

*q = 1234ULL ;

return ;
}

with UInt64 typedef'd in the obvious way. Isn't there an unaligned access
on q now?


Nov 14 '05 #17
Thomas Carter <T.******@nanobots.com> writes:
void f(UInt32 * p)
{
UInt64 * q = (UInt64 *) p ;
*q = 1234ULL ;
return ;
}

with UInt64 typedef'd in the obvious way. Isn't there an unaligned access
on q now?


Yes, now there is. However, that code is now wrong, or at least
assumes that the caller passes a pointer to UInt64 without requiring
the caller to do so (which is a bad idea(TM)).

You claimed that such code is common (it isn't and it's obviously
broken) and that you've found such code in OpenSSL (did you?).

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Nov 14 '05 #18
On Sun, 19 Jun 2005 21:20:26 -0700, Paul Pluzhnikov wrote:
Thomas Carter <T.******@nanobots.com> writes:
void f(UInt32 * p)
{
UInt64 * q = (UInt64 *) p ;
*q = 1234ULL ;
return ;
}

with UInt64 typedef'd in the obvious way. Isn't there an unaligned access
on q now?


Yes, now there is. However, that code is now wrong, or at least
assumes that the caller passes a pointer to UInt64 without requiring
the caller to do so (which is a bad idea(TM)).

You claimed that such code is common (it isn't and it's obviously
broken) and that you've found such code in OpenSSL (did you?).


I admit to being carried away in my assessment here :-( My apologies.
Here is the case I have in mind:

A certain library uses char * throughout, with arbitrary alignments.
Since a char will be 1 byte long (at least, in general, I believe) such
pointers can have any alignment. Now if we have a low-level function that,
for performance reasons, and for a particular architecture, manipulates
unsigned int * as input, then we have a potential problem.

The code above, while obviously wrong, attempts to illustrate such case.

Nov 14 '05 #19

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

Similar topics

4
by: Daniel Polansky | last post by:
Is there a way to open file for writing so that the opening fails if the file already exist? This needs to be atomic operation so that it is possible to use the file as a semaphore. As a result,...
2
by: glen herrmannsfeldt | last post by:
There is a post in comp.lang.java.machine that says that this group doesn't really exist. I wanted to be sure that it did. -- glen
1
by: Mark Richards | last post by:
The solutions for the following problems seems to be simple but I did not found a (convenient) solution: Assume we have a number of elements of the same type under a common parent e.g. <person...
7
by: LT | last post by:
Hello, I have the following problem. I'm using the CMonthCalCtrl calendar control. I need to emphasize some of days, so I'm handling the MCN_GETDAYSTATE event. The problem is when my handler...
2
by: adam | last post by:
hello What query shoul I send to SQL serwer ( in transact SQL language ) to check does some database exist on serwer ? It similar to problem "does some table exist in database" - resolve to it...
8
by: dmitrey | last post by:
howto check does module 'asdf' exist (is available for import) or no? (without try/cache of course) Thx in advance, D.
2
ADezii
by: ADezii | last post by:
Many times when writing VBA code, we find the need to work with Files in one form or another. Whatever the process may be, we need to be sure that a File is present in a specified location. One...
4
by: s0suk3 | last post by:
I wanted to ask for ways to test whether a path exists. I usually use os.path.exists(), which does a stat call on the path and returns True if it succeeds, or False if it fails (catches os.error)....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
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
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...
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...

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.