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

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 #1
55 2980

<fi******@invalid.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.

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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Aug 19 '07 #2
fi******@invalid.com 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.

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.
Aug 19 '07 #3
Harald van Dijk wrote:
fi******@invalid.com 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.
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.
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.
Aug 19 '07 #4
fi******@invalid.com wrote:
>
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?

--
pete
Aug 19 '07 #5
fi******@invalid.com wrote:
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.

--
Thad
Aug 19 '07 #6
"Malcolm McLean" <re*******@btinternet.comwrites:
<fi******@invalid.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?
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.

--
Keith Thompson (The_Other_Keith) 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 19 '07 #7
Keith Thompson wrote:
Harald van Dijk <tr*****@gmail.comwrites:
>fi******@invalid.com 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.
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.
Aug 19 '07 #8

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
"Malcolm McLean" <re*******@btinternet.comwrites:
><fi******@invalid.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 ?

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Aug 19 '07 #9
fi******@invalid.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
"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
Aug 20 '07 #10
Ark Khasin wrote:
fi******@invalid.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
"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
} 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*****@macroexpressions.comwrites:
[...]
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_Keith) 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*****@macroexpressions.comwrote:
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*******@btinternet.comwrote:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>"Malcolm McLean" <re*******@btinternet.comwrites:
>><fi******@invalid.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.orgwrote in message
news:ln************@nuthaus.mib.org...
>"Malcolm McLean" <re*******@btinternet.comwrites:
>><fi******@invalid.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
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?

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?
Even all this is suspect. Nothing forbids saying, for example,
that chars must be aligned on odd addresses, short on multiple of
2, double on multiple of 4, ints on even multiples of 8, pointers
on multiples of 3. Granted the result is peculiar. It is up to
the implementor to satisfy all these for malloc/realloc.

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

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

Aug 20 '07 #20
Keith Thompson wrote:
Ark Khasin <ak*****@macroexpressions.comwrites:
[...]
>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).
Since any pointer is convertable to void*, and back again, I think
that is unnecessary. The whole business is pretty well a mental
exercize anyhow.

--
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 #21
fi******@invalid.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 #22
Harald van =?UTF-8?B?RMSzaw==?= wrote:
>
Ian Collins wrote:
fi******@invalid.com wrote:
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.
What if float and int are different sizes?

And even if they are the same size,
how can you portably find which bit is the sign bit?
Portably, there is no sign bit.
Float representations can be anything.

This expression: (0 float_variable)
gives a compiler all the information it needs
to do the best optimizations conceivable
to determine if float_variable is negative.

--
pete
Aug 20 '07 #23
CBFalconer wrote:
Keith Thompson wrote:
>Ark Khasin <ak*****@macroexpressions.comwrites:
[...]
>>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).

Since any pointer is convertable to void*, and back again, I think
that is unnecessary. The whole business is pretty well a mental
exercize anyhow.
Structure pointers are convertable to void *, but function pointers are not.
Aug 20 '07 #24
On Mon, 20 Aug 2007 14:44:24 -0400, CBFalconer wrote:
Keith Thompson wrote:
>Ark Khasin <ak*****@macroexpressions.comwrites:
[...]
>>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).

Since any pointer is convertable to void*, and back again, I think
that is unnecessary. The whole business is pretty well a mental
exercize anyhow.
Function pointers can't. Also, they're not required to have the
same alignment requirements.
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 20 '07 #25
On Mon, 20 Aug 2007 14:41:13 -0400, CBFalconer wrote:

[snip]
2, double on multiple of 4, ints on even multiples of 8, pointers
Every multiple of 8 is even.
Did you mean multiples of 16? If so, it is a very strange way of
saying that.
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 20 '07 #26
CBFalconer <cb********@yahoo.comwrites:
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?

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?

Even all this is suspect. Nothing forbids saying, for example,
that chars must be aligned on odd addresses, short on multiple of
2, double on multiple of 4, ints on even multiples of 8, pointers
on multiples of 3. Granted the result is peculiar. It is up to
the implementor to satisfy all these for malloc/realloc.
Not quite. chars cannot require anything stricter than byte
alignment. Furthermore, your scheme would not allow any legal
alignment for 'struct { char c; short s; }'.

The alignment of any type must be a factor of its size. It's not
required that all alignments are multiples or factors of each other,
but I've never heard of an implementation where they aren't. For
example, if int requires 3-byte alignment and float requires 4-byte
alignment, then a pointer returned by malloc() must point to a chunk
of memory that's at least 12-byte aligned.

--
Keith Thompson (The_Other_Keith) 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 #27
CBFalconer said:
fi******@invalid.com wrote:
>[...] 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?
....such as:

return value < 0;

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 20 '07 #28
CBFalconer <cb********@yahoo.comwrites:
Keith Thompson wrote:
>Ark Khasin <ak*****@macroexpressions.comwrites:
[...]
>>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).

Since any pointer is convertable to void*, and back again, I think
that is unnecessary. The whole business is pretty well a mental
exercize anyhow.
Suppose you want to implement something similar to malloc() (say, an
allocator that allocates chunks of a statically allocated buffer). If
you could reliably declare a type 'strict_align_t', you could declare
the buffer as:

union {
strict_align_t dummy;
unsigned char data[HEAP_SIZE];
} the_heap;

and allocate data from the_heap.data.

--
Keith Thompson (The_Other_Keith) 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 #29
Keith Thompson wrote:
<snip>
The alignment of any type must be a factor of its size.
<snip>

struct {short a, b, c;} on my machine (ARM7) is 2 bytes aligned and its
size is 6.
Aug 20 '07 #30
Ark Khasin wrote:
Keith Thompson wrote:
<snip>
>The alignment of any type must be a factor of its size.
<snip>

struct {short a, b, c;} on my machine (ARM7) is 2 bytes aligned and its
size is 6.
And 2 is a factor of 6, is it not?
Aug 20 '07 #31
Peter J. Holzer wrote:
On 2007-08-20 03:47, Ark Khasin <ak*****@macroexpressions.comwrote:
>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
Not so, IMHO.
A "number" is not a collection of bits but a member of a set on which
common algebraic operations are defined. (And it may not matter how it
is represented).
There are 4 functions defined on addresses in C:
- typecast, and, for a non-void* type (operation: results in an address)
- adding a number (in the common sense of the word) (operation)
- subtracting a number (operation)
- subtracting two addresses (with a non-address result, so it it is not
an operation)
I don't see how it makes addresses include a number and what that is
supposed to mean
Again, I generally "know" how computers work, but I don't think this
knowledge is portable to the C model of addresses.
-- Ark
Aug 20 '07 #32
Harald van Dijk wrote:
Ark Khasin wrote:
>Keith Thompson wrote:
<snip>
>>The alignment of any type must be a factor of its size.
<snip>

struct {short a, b, c;} on my machine (ARM7) is 2 bytes aligned and its
size is 6.
And 2 is a factor of 6, is it not?
Oops. Made a fool of myself. Sorry

Aug 20 '07 #33
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>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?

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?

Even all this is suspect. Nothing forbids saying, for example,
that chars must be aligned on odd addresses, short on multiple of
2, double on multiple of 4, ints on even multiples of 8, pointers
on multiples of 3. Granted the result is peculiar. It is up to
the implementor to satisfy all these for malloc/realloc.

Not quite. chars cannot require anything stricter than byte
alignment. Furthermore, your scheme would not allow any legal
alignment for 'struct { char c; short s; }'.

The alignment of any type must be a factor of its size. It's not
required that all alignments are multiples or factors of each other,
but I've never heard of an implementation where they aren't. For
example, if int requires 3-byte alignment and float requires 4-byte
alignment, then a pointer returned by malloc() must point to a chunk
of memory that's at least 12-byte aligned.
I am not pushing "my scheme", just pointing out that the proposed
means of discovering alignment requirements are not necessarily
viable.

--
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 #34
fi******@invalid.com wrote:
CBFalconer wrote:
>fi******@invalid.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?

Prehaps you are unaware how slow a floatingpoint comparison is
versus bitwise operations on an int?
So what? I would rather have the program do what it was designed
for, than do something else quicker.

--
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 #35
CBFalconer wrote:
>
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
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?

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?

Even all this is suspect. Nothing forbids saying, for example,
that chars must be aligned on odd addresses, short on multiple of
2, double on multiple of 4, ints on even multiples of 8, pointers
on multiples of 3. Granted the result is peculiar. It is up to
the implementor to satisfy all these for malloc/realloc.
Not quite. chars cannot require anything stricter than byte
alignment. Furthermore, your scheme would not allow any legal
alignment for 'struct { char c; short s; }'.

The alignment of any type must be a factor of its size. It's not
required that all alignments are multiples or factors of each other,
but I've never heard of an implementation where they aren't. For
example, if int requires 3-byte alignment and float requires 4-byte
alignment, then a pointer returned by malloc() must point to a chunk
of memory that's at least 12-byte aligned.

I am not pushing "my scheme", just pointing out that the proposed
means of discovering alignment requirements are not necessarily
viable.
Type punning is not what C is all about anyway.

C is designed to allow any object to be considered
as an array of unsigned char (easily).
But aside from that, aiming a pointer to one type,
at an object of another type,
is *supposed to be* tricky business.

--
pete
Aug 20 '07 #36
CBFalconer <cb********@yahoo.comwrites:
Keith Thompson wrote:
>CBFalconer <cb********@yahoo.comwrites:
[...]
>>Even all this is suspect. Nothing forbids saying, for example,
that chars must be aligned on odd addresses, short on multiple of
2, double on multiple of 4, ints on even multiples of 8, pointers
on multiples of 3. Granted the result is peculiar. It is up to
the implementor to satisfy all these for malloc/realloc.

Not quite. chars cannot require anything stricter than byte
alignment. Furthermore, your scheme would not allow any legal
alignment for 'struct { char c; short s; }'.

The alignment of any type must be a factor of its size. It's not
required that all alignments are multiples or factors of each other,
but I've never heard of an implementation where they aren't. For
example, if int requires 3-byte alignment and float requires 4-byte
alignment, then a pointer returned by malloc() must point to a chunk
of memory that's at least 12-byte aligned.

I am not pushing "my scheme", just pointing out that the proposed
means of discovering alignment requirements are not necessarily
viable.
I didn't mean to imply that you're pushing what I called your "scheme"
(I think "hypothetical implementation" would have been a better choice
of words), and I agree with your conclusion. I was just pointing out
a conforming implementation cannot require odd addresses for chars.
The rest of your hypothetical requirements would be perfectly ok, as
long as each type's size is a multiple of its alignment (arrays can't
have gaps).

It might have been useful for the standard to define a macro
ALIGN_MAX, specifying the maximum alignment of any type (typically 4
or 8), and perhaps a typedef for some arbitrary type that requires
that alignment. Most code wouldn't need it, but it would make it
possible to write a portable malloc-like function.

--
Keith Thompson (The_Other_Keith) 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 #37
fi******@invalid.com wrote:
On 20 Aug 2007 at 12:31, CBFalconer wrote:
>fi******@invalid.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?

Prehaps you are unaware how slow a floatingpoint comparison is versus
bitwise operations on an int?
Perhaps you are unaware that the presence of floating-
point operands in an expression does not necessarily imply
the use of floating-point hardware to evaluate them?

Perhaps you are unaware that it may take considerable
time to store a `value' already resident in an F-P register
and re-fetch it into the integer ALU?

Perhaps you are unaware that the compilers' optimizers
exploit platform-specific and non-portable knowledge that is
not expressible in C source code?

True story: I once found myself porting a Postscript
engine a PPOE had acquired. The code dealt with a lot of
`float' values, and had apparently at some time in its
history been targeted for a machine where F-P was slow --
maybe simulated in software. In the pre-Standard C of the
time, `float' function arguments were always promoted to
`double' and then demoted to `float' again, and someone had
figured out that this took a lot of unnecessary time in the
F-P simulator. So the functions that would ordinarily have
taken `float' arguments instead looked like

func(ptr)
int *ptr; /* it's K&R style, remember? */
{
float val = *((float*)ptr);
...
}

.... and the calls all looked like

float x;
x = 42.0f;
func (&x);

This may have been an optimization for the F-P-weak
original target, but it was a mighty pessimization for the
F-P-capable machine I was trying to port it to. Practically
every function call involved mutual stalls between the integer
and F-P pipelines, each waiting for an operand the other had
yet to deliver ... Gaaaahhh, what a mess! The code's authors
may have had excellent reason to play this ugly game, and I
didn't curse them for it -- but boy, oh, boy, did I curse and
curse and curse those at my company who decided to buy it.

Write what you mean. Don't be clever until measurement
proves you need to be.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 21 '07 #38
Ark Khasin <ak*****@macroexpressions.comwrites:
[...]
[I think of writing a memory manager as of a platform-specific
activity, so the whole discussion would be not applicable.]
[...]

But if it were possible to determine portably (a) the implementation's
strictest alignment, and (b) a type that requires that alignment, then
you *could* write a memory manager (that allocates chunks of a static
array) in portable C.

--
Keith Thompson (The_Other_Keith) 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 21 '07 #39
On 2007-08-20 21:09, Ark Khasin <ak*****@macroexpressions.comwrote:
Peter J. Holzer wrote:
>On 2007-08-20 03:47, Ark Khasin <ak*****@macroexpressions.comwrote:
>>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 ...
Not so, IMHO.
First, let me state that I probably misunderstood you. I thought you
were arguing that addresses *are* numbers and I tried to explain that
they are not.
A "number" is not a collection of bits but a member of a set on which
common algebraic operations are defined. (And it may not matter how it
is represented).
Yes. And you can *interpret* any collection of bits as such a number (in
fact, you aren't restricted to bits: You can interpret any string of
symbols as a number: See for example Gödel numbers for an interesting
use). Then you can perform arithmetic operations on them and get another
number. Whether representing this number as bits and re-interpreting
these bits as whatever the original collection as bits was yields a
useful result is besides the point. (If you interpret a pointer as a
number and multiply it by three and then re-interpret the result as a
pointer, you generally won't get a valid pointer. Similarly, if you
compute the Gödel number of a mathematical theorem and multiply it by
three, you usually won't get the Gödel number of another mathematical
theorem).
There are 4 functions defined on addresses in C:
- typecast, and, for a non-void* type (operation: results in an address)
- adding a number (in the common sense of the word) (operation)
- subtracting a number (operation)
- subtracting two addresses (with a non-address result, so it it is not
an operation)
I don't see how it makes addresses include a number and what that is
supposed to mean
Lets use a concrete example. On the 80286, a (32 bit) pointer consisted
of 4 fields:

* An offset (16 bits)
* A privilege level (2 bits)
* A descriptor table selector (1 bit)
* An index into the descriptor table (13 bits)

As far as the compiler (and functions which care about pointer
internals, such as malloc/free) is concerned, the latter three fields
are not numbers: There is no arithmetic which can be usefully performed
on them.

But the first field, the offset, is an integral number: Pointer
arithmetic is just ordinary integer arithmetic on the offset field, you
can align a pointer by simple arithmetic, etc.

So, on the 80286, a pointer contains a number, but it isn't a number. If
you interpret it as a 32-bit number, the results of many operations
won't be useful.

I don't claim to know all (or even most) architectures on which C
compilers exist, but I'm rather sure that on all of them a pointer
contains a part on which arithmetic can be performed, since otherwise
pointer arithmetic would be rather hard to implement. But as a C
programmer I (normally) don't know or care where that part is or how it
is encoded.

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 21 '07 #40
"Peter J. Holzer" <hj*********@hjp.atwrites:
[...]
Lets use a concrete example. On the 80286, a (32 bit) pointer consisted
of 4 fields:

* An offset (16 bits)
* A privilege level (2 bits)
* A descriptor table selector (1 bit)
* An index into the descriptor table (13 bits)

As far as the compiler (and functions which care about pointer
internals, such as malloc/free) is concerned, the latter three fields
are not numbers: There is no arithmetic which can be usefully performed
on them.

But the first field, the offset, is an integral number: Pointer
arithmetic is just ordinary integer arithmetic on the offset field, you
can align a pointer by simple arithmetic, etc.

So, on the 80286, a pointer contains a number, but it isn't a number. If
you interpret it as a 32-bit number, the results of many operations
won't be useful.

I don't claim to know all (or even most) architectures on which C
compilers exist, but I'm rather sure that on all of them a pointer
contains a part on which arithmetic can be performed, since otherwise
pointer arithmetic would be rather hard to implement. But as a C
programmer I (normally) don't know or care where that part is or how it
is encoded.
C pointers do behave in a number-like fashion in some ways, but the
semantics defined by the C standard do not require pointers to
*contain* numbers. Adding an integer to a pointer, for example, may
involve adding the integer to the integer-like part of the pointer
(which may be an offset field or the entire pointer), but it may not.
In particular, there's no standard way to *extract* a numeric value
from a pointer.

It's likely to be as you describe for all real-world implementations,
but I'll bet the DS9K does something quite tricky (and conforming, of
course) to avoid storing any meaningful numeric data in its pointers.

--
Keith Thompson (The_Other_Keith) 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 21 '07 #41
"Peter J. Holzer" wrote:
>
.... snip ...
>
compute the Gödel number of a mathematical theorem and multiply
it by three, you usually won't get the Gödel number of another
mathematical theorem).
What in the devil is a 'GAFdel' number. That is very roughly the
appearence here of that expresssion.

--
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 22 '07 #42
CBFalconer <cb********@yahoo.comwrites:
"Peter J. Holzer" wrote:
>>
... snip ...
>>
compute the Gödel number of a mathematical theorem and multiply
it by three, you usually won't get the Gödel number of another
mathematical theorem).

What in the devil is a 'GAFdel' number. That is very roughly the
appearence here of that expresssion.
In my newsreader, it looked like "Godel", with an umlaut (two dots)
over the 'o', as it was intended to. The article used a UTF-8
encoding, which of course not all newsreaders are going to recognize.
Since Peter is posting from Austria (.at), I'm sure it's easy to
forget that not everyone can cope with umlauts.

--
Keith Thompson (The_Other_Keith) 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 22 '07 #43
On 2007-08-22 04:04, Keith Thompson <ks***@mib.orgwrote:
CBFalconer <cb********@yahoo.comwrites:
>"Peter J. Holzer" wrote:
>>compute the Gödel number of a mathematical theorem and multiply
it by three, you usually won't get the Gödel number of another
mathematical theorem).

What in the devil is a 'GAFdel' number. That is very roughly the
appearence here of that expresssion.

In my newsreader, it looked like "Godel", with an umlaut (two dots)
over the 'o', as it was intended to. The article used a UTF-8
encoding, which of course not all newsreaders are going to recognize.
Since Peter is posting from Austria (.at), I'm sure it's easy to
forget that not everyone can cope with umlauts.
I don't forget that, but I prefer correct spelling over indulging
users of outdated software. MIME is now 15 years old, UTF-8 not much
younger, so I do think that if somebody still uses software which
doesn't support them, it's their problem and not the the problem of
everybody else. (Incidentally, the newsreader I use doesn't implement
MIME completely, either. But if I come across a posting which uses a
missing feature (e.g. multipart), then I'll ignore the posting or fix it
manually[0] - I won't complain to the poster).

hp

[0] And if there are too many such postings, I'll fix the newsreader.

--
_ | 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 22 '07 #44
"Peter J. Holzer" wrote:
Keith Thompson <ks***@mib.orgwrote:
>CBFalconer <cb********@yahoo.comwrites:
.... snip ...
>>>
What in the devil is a 'GAFdel' number. That is very roughly the
appearence here of that expresssion.

In my newsreader, it looked like "Godel", with an umlaut (two dots)
over the 'o', as it was intended to. The article used a UTF-8
encoding, which of course not all newsreaders are going to recognize.
Since Peter is posting from Austria (.at), I'm sure it's easy to
forget that not everyone can cope with umlauts.

I don't forget that, but I prefer correct spelling over indulging
users of outdated software. MIME is now 15 years old, UTF-8 not much
younger, so I do think that if somebody still uses software which
doesn't support them, it's their problem and not the the problem of
everybody else. (Incidentally, the newsreader I use doesn't implement
MIME completely, either. But if I come across a posting which uses a
missing feature (e.g. multipart), then I'll ignore the posting or fix it
manually[0] - I won't complain to the poster).
I asked for information. I don't recall complaining.

--
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 23 '07 #45
On 2007-08-22 21:25, CBFalconer <cb********@yahoo.comwrote:
"Peter J. Holzer" wrote:
>Keith Thompson <ks***@mib.orgwrote:
>>CBFalconer <cb********@yahoo.comwrites:
What in the devil is a 'GAFdel' number.
[...]
I asked for information. I don't recall complaining.
Ah sorry. I was taking for granted that everbody who's into programming
had heard of Kurt Goedel and would recognize the name even if it was
slightly garbled (but then I'm used to that kind of garbling - character
encoding problems have been me ever since I first turned on an Apple UA
(sorry, Apple ][) 23 years ago). So I didn't consider that you were
really asking for information.

hp

PS: http://en.wikipedia.org/wiki/Kurt_g%C3%B6del
http://en.wikipedia.org/wiki/G%C3%B6...eness_theorems

--
_ | 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 23 '07 #46
pete <pf*****@mindspring.comwrote:
Harald van =?UTF-8?B?RMSzaw==?= wrote:

Ian Collins wrote:
fi******@invalid.com wrote:
>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.
>>
What if float and int are different sizes?
And even if they are the same size,
how can you portably find which bit is the sign bit?

Portably, there is no sign bit.
Float representations can be anything.
That's debatable. There must at least be a sign bit in the conceptual
floating point model; I would be very surprised at an implementation of
that model which didn't have an actual sign bit in its representation,
and I'm not at all sure that such an implementation would be conforming.
This expression: (0 float_variable) gives a compiler all the
information it needs to do the best optimizations conceivable to
determine if float_variable is negative.
That much is, in any case, true and good advise.

Richard
Aug 23 '07 #47
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
pete <pf*****@mindspring.comwrote:
[...]
>Portably, there is no sign bit.
Float representations can be anything.

That's debatable. There must at least be a sign bit in the conceptual
floating point model; I would be very surprised at an implementation of
that model which didn't have an actual sign bit in its representation,
and I'm not at all sure that such an implementation would be conforming.
In IEEE format, the exponent is represented in something like
excess-128 notation (depending on the size of the exponent), where
all-bits-zero represents the most negative value, not 0. I can
imagine a floating-point format where the mantissa uses a similar
representation. (I think there might still be a bit that's a reversed
sign bit, with 0 for negative values and 1 for positive values).

I can easily believe that no such representation exists in the real
world, but I don't thinkt the C standard says enough about FP
representations to make it non-conforming. See the DS9K.

[...]

--
Keith Thompson (The_Other_Keith) 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 23 '07 #48
"Peter J. Holzer" <hj*********@hjp.atwrote:
On 2007-08-22 04:04, Keith Thompson <ks***@mib.orgwrote:
CBFalconer <cb********@yahoo.comwrites:
"Peter J. Holzer" wrote:
compute the Gödel number of a mathematical theorem and multiply
it by three, you usually won't get the Gödel number of another
mathematical theorem).

What in the devil is a 'GAFdel' number. That is very roughly the
appearence here of that expresssion.
In my newsreader, it looked like "Godel", with an umlaut (two dots)
over the 'o', as it was intended to. The article used a UTF-8
encoding, which of course not all newsreaders are going to recognize.
Since Peter is posting from Austria (.at), I'm sure it's easy to
forget that not everyone can cope with umlauts.

I don't forget that, but I prefer correct spelling over indulging
users of outdated software. MIME is now 15 years old, UTF-8 not much
younger, so I do think that if somebody still uses software which
doesn't support them, it's their problem and not the the problem of
everybody else.
MIME is Multi-part Internet _Mail_ Extensions, though. This is not mail,
it's Usenet. More to the point, it's a Big-7 Usenet group, where 7-bits,
plain ASCII is still the norm. I'm not complaining about your post, just
as I'm not complaining about Outhouse's Quoted-Illegible making code
unreadable[1]; but any communication problems _are_ the fault of the
party using a protocol which does not belong here, not that of the party
which is Usenet-compliant.

Richard

[1] I just ignore any posts which are too much trouble to decode
Aug 24 '07 #49
Richard Bos wrote:
>
pete <pf*****@mindspring.comwrote:
Harald van =?UTF-8?B?RMSzaw==?= wrote:
>
Ian Collins wrote:
fi******@invalid.com wrote:
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.
>
What if float and int are different sizes?
>
And even if they are the same size,
how can you portably find which bit is the sign bit?
Portably, there is no sign bit.
Float representations can be anything.

That's debatable. There must at least be a sign bit in the conceptual
floating point model;
There must be a sign, in the conceptual floating point model,
but the standard does not say
that there must be a special bit for that.

--
pete
Aug 25 '07 #50

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

Similar topics

4
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...
10
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...
67
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...
7
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...
13
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...
12
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
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;...
2
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...
2
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...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...

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.