473,800 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
18 10079
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*****@NOwrit emeNO.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.******@nanob ots.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(MAGICNUM BER);
unsigned int *p = x + 1;
unsigned i;
if (!x) {
fprintf(stderr, "The allocation of x failed.\n"
"Bailing out ...\n");
exit(EXIT_FAILU RE);
}
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.******@nanob ots.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*****@NOwrit emeNO.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.earth link.net>
Martin Ambuhl <ma*****@earthl ink.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(MAGICNUM BER); .... if (!x) {
fprintf(stderr, "The allocation of x failed.\n"
"Bailing out ...\n");
exit(EXIT_FAILU RE);
}
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.******@nanob ots.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.******@nanob ots.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.******@nanob ots.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
5478
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, the following coding does not do: if (!file.exist()) { // open the file for writing ... A similar function definitely exists for accessing files in
2
1898
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
18909
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 ... myattr="aaa">Paul</person> <person ... myattr="bbb">Peter</person> <person ... myattr="ccc">Karl</person> ..... <person ... myattr="ddd">Stan</person>
7
2424
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 function is called for the first time during initializing of the application. When I'm calling the CMonthCalCtrl::GetMonthRange method there is an assertion error inside that method: ASSERT(::IsWindow(m_hWnd)); It seems that my event handler is...
2
17901
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 is query: use db_silnik IF EXISTS (SELECT * FROM prad) PRINT 'table exist'
8
3760
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
12632
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 Method to guarantee that a File does in fact exist, is to pass the Absolute Path to the File to a Function which will investigate the Path and return a Boolean Value (True/False) indicating whether or not the File exists. The following code segments will...
4
2129
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). But stat calls don't fail only when a path doesn't exist. I see that, at least on Windows, the instance of the exception has an attribute 'errno' set to 2 when it fails because the path doesn't exist. Is it a portable solution to rely on this...
0
10281
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
10256
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
10039
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
9095
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...
0
6824
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
5477
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...
0
5612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4152
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
3
2953
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.