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

Home Posts Topics Members FAQ

Alignment

How to declare a variable guaranteed to have the strictest possible
alignment?

--
The defense attorney was hammering away at the plaintiff:
"You claim," he jeered, "that my client came at you with a broken bottle
in his hand. But is it not true, that you had something in YOUR hand?"
"Yes," the man admitted, "his wife. Very charming, of course,
but not much good in a fight."
Aug 19 '07
55 3040
Ark Khasin wrote:
fi******@invali d.com wrote:
>How to declare a variable guaranteed to have the strictest possible
alignment?

I guess the first question is, what's alignment? C99 3.2 says
"requiremen t that objects of a particular type be located on storage
boundaries with addresses that are particular multiples of a byte address."

That's a motivational explanation (like a math explanation of a set) but
not a definition: addresses are not numbers, and a multiple of a byte
address is... what?

Is it fair to say that all we can say (axiomatically) that
- For a type T there is an alignment good for storing a T object, and
- if T *p is an address good for storing a T object, then so is p+1?
[And then whatever alignment hierarchy the standard defines.]

I cannot imagine why one needs a strictest-aligned variable of static
duration, but the following would work
typedef union {
char c;
short s;
int i;
long l;
float f;
double d;
long double ld;
long long ll;
//and whatever native types I missed
"Aye: There's the rub." -- H, P of D
} strict_align_t;
strict_align_t myvar;
--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 20 '07 #11
Eric Sosman wrote:
Ark Khasin wrote:
>>
I cannot imagine why one needs a strictest-aligned variable of static
duration, but the following would work
typedef union {
char c;
short s;
int i;
long l;
float f;
double d;
long double ld;
long long ll;
//and whatever native types I missed

"Aye: There's the rub." -- H, P of D
>} strict_align_t;
strict_align _t myvar;
Of course. Will the addition of "void *v; const void *cv;" do it?

And, taking off my assembler motley, may I ask again what is alignment?
Is there some sort of conspiracy and addresses in fact /are/ some sort
of numbers?
Aug 20 '07 #12
Ark Khasin wrote:
Eric Sosman wrote:
>Ark Khasin wrote:
>>>
I cannot imagine why one needs a strictest-aligned variable of static
duration, but the following would work
typedef union {
char c;
short s;
int i;
long l;
float f;
double d;
long double ld;
long long ll;
//and whatever native types I missed

"Aye: There's the rub." -- H, P of D
>>} strict_align_t;
strict_align_ t myvar;
Of course. Will the addition of "void *v; const void *cv;" do it?
Don't forget function pointers.

--
Ian Collins.
Aug 20 '07 #13
Ian Collins wrote:
Ark Khasin wrote:
>Eric Sosman wrote:
>>Ark Khasin wrote:
I cannot imagine why one needs a strictest-aligned variable of static
duration, but the following would work
typedef union {
char c;
short s;
int i;
long l;
float f;
double d;
long double ld;
long long ll;
//and whatever native types I missed
"Aye: There's the rub." -- H, P of D

} strict_align_t;
strict_align _t myvar;
Of course. Will the addition of "void *v; const void *cv;" do it?
Don't forget function pointers.
Thanks! I've missed that C99: J.5.7 is informative, not normative.
Aug 20 '07 #14
Ark Khasin <ak*****@macroe xpressions.comw rites:
[...]
I cannot imagine why one needs a strictest-aligned variable of static
duration, but the following would work
typedef union {
char c;
short s;
int i;
long l;
float f;
double d;
long double ld;
long long ll;
//and whatever native types I missed
} strict_align_t;
strict_align_t myvar;

Unsigned and complex stuff can be inferred to fit this alignment.
It's very likely to work, but it's certainly not guaranteed to work.
There are arbitrarily many types, and any of them could theoretically
have stricter alignment requirements than any of the types you've
listed.

If you're going to use that approach, you should include at least a
few pointer types (void*, a struct pointer, and a function pointer).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 20 '07 #15
On 2007-08-20 03:47, Ark Khasin <ak*****@macroe xpressions.comw rote:
And, taking off my assembler motley, may I ask again what is alignment?
Is there some sort of conspiracy and addresses in fact /are/ some sort
of numbers?
Addresses aren't numbers, but each address includes at least one number
(if it didn't, pointer arithmetic would not be possible). In a flat
address space (the usual case today), the address consists only of a
single number. In a segmented address space, it contains (at least) two
numbers: A segment number and a segment offset, where arithmetic is
often only meaningful on the offset. An address can contain other
information (type information, flags, etc.).

Of course you can interpret any collection of bits as a number ...

hp
--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | hj*@hjp.at |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
Aug 20 '07 #16
On 2007-08-19 22:07, Malcolm McLean <re*******@btin ternet.comwrote :
"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
>"Malcolm McLean" <re*******@btin ternet.comwrite s:
>><fi******@inv alid.comwrote in message
news:sl****** **************@ nospam.invalid. ..
How to declare a variable guaranteed to have the strictest possible
alignment?
In practise, align to a double.
[...]
>What if long double has stricter alignment requirements than double?
Computer like things in units of powers of two.
doubles are almost always 64 bits, in fact have to be if IEEE. long doubles
are 80.
No. long doubles are 80 on Intel x86. I've seen 96 bit and 128 bit long
doubles. It is entirely possible that a 128 bit long double needs to be
aligned on a 128 bit boundary.

hp
--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | hj*@hjp.at |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
Aug 20 '07 #17
On Sun, 19 Aug 2007 23:07:43 +0100, Malcolm McLean wrote:
"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
>"Malcolm McLean" <re*******@btin ternet.comwrite s:
>><fi******@inv alid.comwrote in message
news:sl****** **************@ nospam.invalid. ..
How to declare a variable guaranteed to have the strictest possible
alignment?

In practise, align to a double.
eg

union alignedint
{
double dummy;
int x;
}

x will now have double alignement.

What if long double has stricter alignment requirements than double?
Computer like things in units of powers of two. doubles are almost
always 64 bits, in fact have to be if IEEE. long doubles are 80. So the
alignment requirements for double are likely to be stricter. However you
are right, in practise isn't the same as "by the standard". It might
even be storing up trouble for the future to advise such a thing. I
haven't made my mind up about this one. How about a new type. align_t ?
When are you going to stop giving misleading advice? Please, do
go back to your specialty - C is NOT it.

Aug 20 '07 #18
Ark Khasin wrote:
Eric Sosman wrote:
>Ark Khasin wrote:
>>>
I cannot imagine why one needs a strictest-aligned variable of static
duration, but the following would work
typedef union {
char c;
short s;
int i;
long l;
float f;
double d;
long double ld;
long long ll;
//and whatever native types I missed

"Aye: There's the rub." -- H, P of D
>>} strict_align_t;
strict_align_ t myvar;
Of course. Will the addition of "void *v; const void *cv;" do it?
It can't hurt (but it's not necessary to add both; the
qualifiers don't affect the alignment requirement). Include
a function pointer while you're about it, and some struct
types (a struct might require stricter alignment than its
members), and size_t, and intmax_t, and ... The "rub" is
that the list of possible types is potentially infinite.

<off-topic>

There's another "rub," too, which is that some alignment
requirements of the platform may not correspond to any C type
at all. "Page alignment," for example, is likely to be hard
to achieve with this approach. On systems that support multiple
page sizes, testing for page alignment uses a divisor specific to
the "arena" containing the address. A static, location-independent
scheme like the above cannot work on such systems, even if you can
find a page-sized C data type.

</off-topic>
And, taking off my assembler motley, may I ask again what is alignment?
Is there some sort of conspiracy and addresses in fact /are/ some sort
of numbers?
Peter J. Holzer's explanation elsethread is good.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 20 '07 #19
fi******@invali d.com wrote:
>
.... snip ...
>
But some type punning can be done in completely Standard C. For
example, deciding whether a float is positive or negative by
treating it as an int and extracting the sign bit uses only
bit-shifting which is a Standard C operator.
But the number of bits in an int isn't standard. What's wrong in:

if (value < 0) return 1;
else return 0;

or the equivalent in other terms?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 20 '07 #20

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

Similar topics

4
17680
by: Shashi | last post by:
Can somebody explain how the byte alignment for structures work, taking the following example and considering: byte of 1 Byte word of 2 Bytes dword of 4 Bytes typedef struct { byte a; word b;
10
3222
by: j0mbolar | last post by:
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?
67
10701
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): 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. Does it mean that *all* structure (or union) types have the same alignment? Eg. type
7
2028
by: Earl | last post by:
Any known fixes for the wacky right-alignment bug in the WinForms datagrid (VS2003)? I've tried Ken's workaround (http://www.windowsformsdatagridhelp.com/default.aspx?ID=4bfab32d-9cff-4f5c-ba95-49bb9074a8bc), but I get no alignment at all when calling the class. George Shephard's site, while imminently useful, does not have an anwer for this issue.
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'."
12
818
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: #include <stdlib.h> #define MAGIC_NUMBER 64 void *my_malloc (size_t n) { char *result = malloc (n + MAGIC_NUMBER);
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... };
2
3788
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long Align; union header { struct {
2
20145
by: uamusa | last post by:
I am Dynamically generating a proposal(report) in MS Word. By default the Paragraph Alignment is "Left". For the First 6 Paragraphs I set the Alignment to "Center", and then when attempting to switch back to "Left" aligned for remaining paragraphs, the text within the Word document remained Centered. I'm using: VS2008 w/ .NET Framework 3.5, Microsoft.Office.Interop.Word Version 12.0.0.0, and I'm creating the document as a 97-2003 document...
0
8113
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8051
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,...
1
8203
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
8378
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
6981
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
6047
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...
1
2517
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
1
1637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1373
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.