Alignment

F

fishpond

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."
 
M

Malcolm McLean

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.

In strictly correct terms, use compiler pragmas to achieve what you want.
double isn't guaranteed to be the most strictly aligned type and there is no
portable mechnaism for finding it.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

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

Given that any object type can be contained in a structure or a union, and
all structure and union types have the same alignment requirements, any
structure or union type will do.

However, why do you need this? There's very little in standard C that you
can do with knowledge of alignment of non-dynamic objects. Even if you have
a structure containing an array of four chars, which is properly aligned
for an int, and sizeof(int)==4 on your platform, you're still not allowed
to access this structure as were it an int.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Harald said:
Given that any object type can be contained in a structure or a union, and
all structure and union types have the same alignment requirements, any
structure or union type will do.

Sorry, this is of course incorrect. I was thinking of pointers to
structures. The below is still true, though you'd need to have another way
of making the structure properly aligned for it to be relevant.
 
P

pete

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

Allocated objects have strictest alignment.

Does it have to be a declared variable?
 
K

Keith Thompson

Harald van Dijk said:
Given that any object type can be contained in a structure or a union, and
all structure and union types have the same alignment requirements, any
structure or union type will do.
[...]

Not true. All *pointers* to structure or union types have the same
alignment requirements, but that refers only to the alignment of
pointer objects, not to the alignment of what they point to. It's
entirely possible for 'struct { int n; }' to have stricter alignment
than 'struct { char c; }', for example.

A union known to contain a member with the strictest possible
alignment requirement would do the trick, but there's no portable way
to determine this. A union containing members of types int, long,
long long (if C99), float, double, long double, void*, and maybe a few
other pointer types (including function pointer types) is very likely
to work, but it's not guaranteed.

If you want something guaranteed to be aligned for any type, use
malloc(). Or check your system's documentation.
 
K

Keith Thompson

Malcolm McLean said:
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?
In strictly correct terms, use compiler pragmas to achieve what you
want. double isn't guaranteed to be the most strictly aligned type and
there is no portable mechnaism for finding it.

In strictly correct terms, (or, as I prefer to say, "correct terms")
there is no portable way to do this; the language doesn't define any
pragmas for this purpose.
 
F

fishpond

Harald van Dijk said:
Given that any object type can be contained in a structure or a union, and
all structure and union types have the same alignment requirements, any
structure or union type will do.
[...]

Not true. All *pointers* to structure or union types have the same
alignment requirements, but that refers only to the alignment of
pointer objects, not to the alignment of what they point to. It's
entirely possible for 'struct { int n; }' to have stricter alignment
than 'struct { char c; }', for example.

A union known to contain a member with the strictest possible
alignment requirement would do the trick, but there's no portable way
to determine this. A union containing members of types int, long,
long long (if C99), float, double, long double, void*, and maybe a few
other pointer types (including function pointer types) is very likely
to work, but it's not guaranteed.

If you want something guaranteed to be aligned for any type, use
malloc(). Or check your system's documentation.

It's hard to believe that something this simple can't be achieved in
Standard C. I mean, if that's true, how would you ever get type punning
going?
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Keith said:
Harald van Dijk said:
Given that any object type can be contained in a structure or a union,
and all structure and union types have the same alignment requirements,
any structure or union type will do.
[...]

Not true. All *pointers* to structure or union types have the same
alignment requirements, but that refers only to the alignment of
pointer objects, not to the alignment of what they point to. It's
entirely possible for 'struct { int n; }' to have stricter alignment
than 'struct { char c; }', for example.

To clarify beyond the message I sent earlier: I used to believe that all
structures must have the same alignment requirements. I have never read a
satisfactory explanation of how the representation requirements for
pointers to structures do not imply this, I accept that the intent was that
different structures may have different alignment requirements, and all
that has nothing to do with my first message in this thread, which was
nothing more a slip of the mind unrelated to that old argument.

Or, to shorten it somewhat: agreed.
 
T

Thad Smith

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

How to ask a question without a subject and verb?

I assume you mean something like "how can I declare a variable
guaranteed to have the strictest possible alignment?".

To get alignment suitable to any type, dynamically allocate it.
Otherwise, you can declare a union containing all the types that must be
aligned properly.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

It's hard to believe that something this simple can't be achieved in
Standard C. I mean, if that's true, how would you ever get type punning
going?

Type punning isn't something you should do in code that's meant to be
portable, and if the code isn't meant to be portable, it shouldn't matter
as much that the behaviour is defined by your implementation rather than by
the standard.
 
M

Malcolm McLean

Keith Thompson said:
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 ?
 
R

Robert Gamble

Harald van D k said:
(e-mail address removed) wrote:
How to declare a variable guaranteed to have the strictest possible
alignment?
Given that any object type can be contained in a structure or a union, and
all structure and union types have the same alignment requirements, any
structure or union type will do. [...]

Not true. All *pointers* to structure or union types have the same
alignment requirements, but that refers only to the alignment of
pointer objects, not to the alignment of what they point to. It's
entirely possible for 'struct { int n; }' to have stricter alignment
than 'struct { char c; }', for example.
A union known to contain a member with the strictest possible
alignment requirement would do the trick, but there's no portable way
to determine this. A union containing members of types int, long,
long long (if C99), float, double, long double, void*, and maybe a few
other pointer types (including function pointer types) is very likely
to work, but it's not guaranteed.
If you want something guaranteed to be aligned for any type, use
malloc(). Or check your system's documentation.

It's hard to believe that something this simple can't be achieved in
Standard C. I mean, if that's true, how would you ever get type punning
going?

If you want to pun types you can create a union with members of the
types you want to participate in the punning. You are guaranteed that
each member starts at the same address which is properly aligned for
all the members, you also bypass the aliasing rules that would come
into play with the alternative of casting through a pointer to a
different type. The only thing you need to worry about is
representation, if the punned type turns out to be a trap
representation and you access it the behavior is undefined.

Robert Gamble
 
A

Ark Khasin

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
"requirement 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
} strict_align_t;
strict_align_t myvar;

Unsigned and complex stuff can be inferred to fit this alignment.

-- Ark
 
E

Eric Sosman

Ark said:
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
"requirement 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
 
I

Ian Collins

It's hard to believe that something this simple can't be achieved in
Standard C. I mean, if that's true, how would you ever get type punning
going?
The fact that there isn't a simple standard way hits to the fact that
specific alignments are seldom required. What's wrong or complicated
with Keith's suggestion?
 
A

Ark Khasin

Eric said:
"Aye: There's the rub." -- H, P of D
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?
 
K

Keith Thompson

Ark Khasin said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top