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

Alignment of pointer to void

Hi,

Assuming that a pointer to void is always a multiple of 4 provides an
opportunity to use as tags the two bits that are always 0. Knowing that
several experts on the ISO99 C standard should be monitoring this
group, I would like to know if this assumption complies with this
standard before using it in a project. Also, if this is the case, where
does the standard state this?

Best regards,

David.
--

Jun 27 '06 #1
10 4586
David Deharbe wrote:

Hi,

Assuming that a pointer to void is always a multiple of 4 provides an
opportunity to use as tags the two bits that are always 0.
Knowing that
several experts on the ISO99 C standard should be monitoring this
group, I would like to know if this assumption complies with this
standard before using it in a project.


It doesn't.

N869
6.2.5 Types
[#27] A pointer to void shall have the same representation
and alignment requirements as a pointer to a character type.

and that's all that the standard says about the representation
of pointer to void.

--
pete
Jun 27 '06 #2
David Deharbe wrote:
Assuming that a pointer to void is always a multiple of 4 provides an
opportunity to use as tags the two bits that are always 0. Knowing that
several experts on the ISO99 C standard should be monitoring this
group, I would like to know if this assumption complies with this
standard before using it in a project. Also, if this is the case, where
does the standard state this?

If by "complies" you mean "is guaranteed by", then no, this is not the case.

If you mean that it's legal for an implementation to have pointers to void
for which the binary representation has the two least significant bits
unset, then yes.

If you mean that it's legal for an implementation to have pointers to void
for which the binary representation *always* has the two least significant
bits unset, then yes.

If you're asking whether it's possible to write a C program that assumes
either is the case, then also yes, but with the provision that such a C
program cannot be strictly conforming. Conversions from pointers to integers
and back are implementation-defined at best and undefined at worst.

If you're asking in general whether it's a good idea to do this, then
emphatically no. Your program will be chained to a relatively small set of
circumstances, it will have to be careful doing low-level manipulations, and
the gain is questionable. If you need to store extra information with a
pointer, consider using a struct for each individual pointer or a hashtable.
(Solving the problem of how to hash pointers to void as portably as possible
is still more rewarding than manipulating the representation directly.)

S.
Jun 27 '06 #3
pete <pf*****@mindspring.com> wrote:
David Deharbe wrote:
Assuming that a pointer to void is always a multiple of 4 provides an
opportunity to use as tags the two bits that are always 0.
Knowing that
several experts on the ISO99 C standard should be monitoring this
group, I would like to know if this assumption complies with this
standard before using it in a project.


It doesn't.

N869
6.2.5 Types
[#27] A pointer to void shall have the same representation
and alignment requirements as a pointer to a character type.

and that's all that the standard says about the representation
of pointer to void.


(In my copy of the standard (ISO/IEC 9899:1999(E),) that is paragraph
6.2.5 - #26, not #27. Wonder what changed.)

A pointer to void returned by malloc(), etc., has stronger alignment
requirements, (see below, probably the source of the wrong
assumption,) but not just any void pointer. It may be a multiple of 4
in a particular environment, but of course any dependency in that fact
will make your code non-portable.

7.20.3
"The pointer returned if the allocation succeeds is suitably aligned
so that it may be assigned to a pointer to any type of object..."
Jun 27 '06 #4
In article <44***********************@news.xs4all.nl> Skarmander <in*****@dontmailme.com> writes:
David Deharbe wrote:
Assuming that a pointer to void is always a multiple of 4 provides an
opportunity to use as tags the two bits that are always 0. Knowing that
several experts on the ISO99 C standard should be monitoring this
group, I would like to know if this assumption complies with this
standard before using it in a project. Also, if this is the case, where
does the standard state this?
.... If you're asking in general whether it's a good idea to do this, then
emphatically no. Your program will be chained to a relatively small set of
circumstances, it will have to be careful doing low-level manipulations, and
the gain is questionable. If you need to store extra information with a
pointer, consider using a struct for each individual pointer or a hashtable.


Indeed. The assumption that for some particular pointer type some low order
bits were always 0 and could be used for administrative purposes lead to
a headache when porting the Korn shell to the Cray.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jun 27 '06 #5
In article <5t********************************@4ax.com> Roberto Waltman <us****@rwaltman.net> writes:
....
7.20.3
"The pointer returned if the allocation succeeds is suitably aligned
so that it may be assigned to a pointer to any type of object..."


But suitable alignment does *not* imply that the low order bits are 0.
For instance, on the Cray it implies that the high order 16 bits are 0
(they contain the byte in word pointer).
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jun 27 '06 #6
Dik T. Winter wrote:

In article <5t********************************@4ax.com> Roberto Waltman <us****@rwaltman.net> writes:
...
> 7.20.3
> "The pointer returned
> if the allocation succeeds is suitably aligned
> so that it may be assigned to a pointer to any type of object..."


But suitable alignment does *not* imply that the low order bits are 0.
For instance, on the Cray it implies that the high order 16 bits are 0
(they contain the byte in word pointer).


The concepts of low order bits or value bits or padding bits,
are not applied to pointers by the standard.

--
pete
Jun 27 '06 #7
"Dik T. Winter" <Di********@cwi.nl> wrote:
Roberto Waltman <us****@rwaltman.net> writes:
...
7.20.3
"The pointer returned if the allocation succeeds is suitably aligned
so that it may be assigned to a pointer to any type of object..."


But suitable alignment does *not* imply that the low order bits are 0.
For instance, on the Cray it implies that the high order 16 bits are 0
(they contain the byte in word pointer).


No disagreement here. What Cray model/line are you referring to?
The Crays are one of the architectures often mentioned in c.l.c as
examples of environments were common (but wrong) assumptions break you
code. I would like to read the C manual for other implementations
"oddities"
Jun 27 '06 #8
In article <ll********************************@4ax.com> Roberto Waltman <us****@rwaltman.net> writes:
"Dik T. Winter" <Di********@cwi.nl> wrote:
Roberto Waltman <us****@rwaltman.net> writes:
...
7.20.3
"The pointer returned if the allocation succeeds is suitably aligned
so that it may be assigned to a pointer to any type of object..."
But suitable alignment does *not* imply that the low order bits are 0.
For instance, on the Cray it implies that the high order 16 bits are 0
(they contain the byte in word pointer).


No disagreement here. What Cray model/line are you referring to?


The Cray-1 and successors (i.e. those based on the original architecture).
The Crays are one of the architectures often mentioned in c.l.c as
examples of environments were common (but wrong) assumptions break you
code. I would like to read the C manual for other implementations
"oddities"


I do not know whether such manuals are available online or offline, one
oddity is that there is no division instruction and the quotient can be
wrong in the two low order bits. In the course of time I have used four
different architectures that would break common assumptions.
(1) Cray-1 and successors. Pointers are (64-bit) word pointers. A char
pointer is constructed by putting the char number in the high order
16 bits. Also no division instruction, so the quotient could be
quite a bit wrong. Integers contain padding bits (the high order
16 bits of the 64 bit word).
(2) Data General MV series. A char pointer would have the low order
24 (I think) bits as byte pointer and the high order 8 bits as
"ring number", which would be non-zero for user programs (and so
NULL is not all bits 0). Any other pointer would have the (16-bit)
word address in the low order 23 bits, next 8 bits for the ring number
and one bit that indicates indirection. On that machine with c a
char pointer, the cast (int *)c was certainly *not* a no-op.
(3) CDC 205. Every pointer was a bit pointer. So a char pointer would
have the lower three bits 0. Also 0.0 (when normalised) would not
be all bits zero. Also on this machine (a == b) == (b == a) could
be false (there was asymmetry in the instruction).
(4) Intel i960. No division instruction, so division could be a bit off
(but not as far off as the Cray).
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jun 27 '06 #9
"Dik T. Winter" <Di********@cwi.nl> writes:
[...]
(1) Cray-1 and successors. Pointers are (64-bit) word pointers. A char
pointer is constructed by putting the char number in the high order
16 bits. Also no division instruction, so the quotient could be
quite a bit wrong. Integers contain padding bits (the high order
16 bits of the 64 bit word).


My experience on the Cray T90 is that the byte offset is stored in the
high order 3 bits of the 64-bit word. (Presumably the other 13 bits
of the top 16 are always 0; you'd never need more than 48 bits to
address all of memory.) So saying that the offset is stored in the
high 16 bits is correct, but imprecise.

("High-order bits" are not, of course, a concept defined by the C
standard for pointers.)

It would be nice if C provided ways to look at the alignments of
pointers without depending on their representation. I think it would
suffice to define a macro MAX_ALIGN specifying the maximum meaningful
alignment, and a "%" operator that takes a pointer and an integer,
defined only where the integer is a power of 2 no greater than
MAX_ALIGN. (But this assumes everything is done in powers of 2, which
might not be the case.)

--
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.
Jun 28 '06 #10
Thanks for all your answers - that was very helpful. Actually, I first
saw this technique in a BDD implementation (i.e.
http://www.cs.cmu.edu/~modelcheck/bdd.html), and was thinking of using
it to implement balanced trees. Since I strive for portability I will
discard this solution.

Best,

David.
--

Jun 28 '06 #11

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

Similar topics

36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
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...
5
by: Hendrik Schober | last post by:
Hi, we just run into the problem, that "default" alignment in the project properies dialog seem to be different. We have a project that's a DLL, which is linked with a couple of LIBs. All are...
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...
3
by: Bill Pursell | last post by:
I have a program that does most of its work traversing a bunch of lists. The lists contain a void *, and I spent some time today replacing the void *'s with a copy of the data at the end of the...
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;...
13
by: Chris Thomasson | last post by:
Here is some info on a C++ allocator prototype I am working on: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/beeee1f61fdbb52c Any tried-and-true techniques for calculating the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.