Q's on pointer sizes

G

Gonçalo Rodrigues

Hi all,

Is it true that all pointer types that can be cast into void* (that
is, all pointer types with the exception of pointers to functions and
pointers to members) have the same size? And if not, do you know of
any examples of platform (OS + compiler) where such happens?

And does standard C++ guarantee the existence of a primitive (signed)
integer type T such that

sizeof(T) == sizeof(void*)

Even assuming the answer to the precedent question is negative (as I
suspect it is), suppose that in a given platform such an integer
primitive type T exists. Does anybody know of a way to single out such
a type, via some template or whatever?

TIA, with my best regards,
G. Rodrigues
 
V

Victor Bazarov

Gonçalo Rodrigues said:
Is it true that all pointer types that can be cast into void* (that
is, all pointer types with the exception of pointers to functions and
pointers to members) have the same size?
Yes.

And if not, do you know of
any examples of platform (OS + compiler) where such happens?

And does standard C++ guarantee the existence of a primitive (signed)
integer type T such that

sizeof(T) == sizeof(void*)

No. Check the documentation for your implementation.
Even assuming the answer to the precedent question is negative (as I
suspect it is), suppose that in a given platform such an integer
primitive type T exists. Does anybody know of a way to single out such
a type, via some template or whatever?

The only way exists is to confer with the documentation. There can be
a type that is non-standard. For example, on Win64, it's __int64.

V
 
R

red floyd

Gonçalo Rodrigues said:
Hi all,

Is it true that all pointer types that can be cast into void*

4.10/2
An rvalue of type "pointer to cv T,", where T is an object type,
can be converted be converted to an rvalue of "pointer to cv void".

In other words, yes, so long as your const/volatileness is preserved.

(that
is, all pointer types with the exception of pointers to functions and
pointers to members)
note: 4.11/2 footnote 52 notes that "In particular, a pointer to member
cannot be converted to a void*".

And does standard C++ guarantee the existence of a primitive (signed)
integer type T such that

5.2.10/4 "A pointer can be explicitly converted to any integral type
large enough to hold it. The mapping function is implementation
defined. [Note: it is intended to be unurprising to those who know the
addressing structure of the underlying machine.]"

So yes, there exists, some type, but its type is implementation defined.

Note also, that 5.2.10/5 implies the reverse conversion exists as well.
sizeof(T) == sizeof(void*)

Even assuming the answer to the precedent question is negative (as I
suspect it is), suppose that in a given platform such an integer
primitive type T exists. Does anybody know of a way to single out such
a type, via some template or whatever?

Again, see above. Implementation defined. Your best bet is with,
#ifdefs, and either manifest defines, or your own architecture specific
defines.
 
A

Andrey Tarasevich

Gonçalo Rodrigues said:
Is it true that all pointer types that can be cast into void*
Yes.

(that
is, all pointer types with the exception of pointers to functions and
pointers to members) have the same size?

No. The language guarantees that for any "regular" pointer type 'T*' the
round-trip conversion T* -> void* -> T* shall produce the original pointer
value. This means that, putting it informally, the amount of information stored
in the 'void*' pointer should be the same _or_ _greater_ than the largest amount
of information stored in any "regular" 'T*' pointer.

On platforms where pointer objects contain no internal padding that will mean
that physical size of 'void*' pointer (sizeof(void*)) will be the same _or_
_greater_ than physical size of any other "regular" pointer.

However, it is possible that on certain platform some pointer type 'T*' contains
lots of internal padding, i.e. parts of the pointer object that do not
participate in forming the actual pointer value. Physical size of such a pointer
can easily be greater than physical size of 'void*', while the above language
requirement will still be met.
And if not, do you know of
any examples of platform (OS + compiler) where such happens?

Hmm... Not immediately.
And does standard C++ guarantee the existence of a primitive (signed)
integer type T such that

sizeof(T) == sizeof(void*)

No. Note also that from purely pedantic point of view the fact that 'sizeof's of
two types are equal does not mean that both types use the same number of
value-forming bits.
Even assuming the answer to the precedent question is negative (as I
suspect it is), suppose that in a given platform such an integer
primitive type T exists. Does anybody know of a way to single out such
a type, via some template or whatever?

I don't think there's a portable way to do it.
 
K

Kai-Uwe Bux

red said:
Gonçalo Rodrigues wrote: [snip]
And does standard C++ guarantee the existence of a primitive (signed)
integer type T such that

5.2.10/4 "A pointer can be explicitly converted to any integral type
large enough to hold it. The mapping function is implementation
defined. [Note: it is intended to be unurprising to those who know the
addressing structure of the underlying machine.]"

So yes, there exists, some type, but its type is implementation defined.

Where in that paragraph did you read that there exists an integral type
large enough to hold a pointer? All it says is that *if* you have such a
type, *then* you can cast. The question *whether* you have such a type is
not addressed as far as I can see.

[snip]


Best

Kai-Uwe Bux
 
R

red floyd

Kai-Uwe Bux said:
red said:
Gonçalo Rodrigues wrote: [snip]
And does standard C++ guarantee the existence of a primitive (signed)
integer type T such that
5.2.10/4 "A pointer can be explicitly converted to any integral type
large enough to hold it. The mapping function is implementation
defined. [Note: it is intended to be unurprising to those who know the
addressing structure of the underlying machine.]"

So yes, there exists, some type, but its type is implementation defined.

Where in that paragraph did you read that there exists an integral type
large enough to hold a pointer? All it says is that *if* you have such a
type, *then* you can cast. The question *whether* you have such a type is
not addressed as far as I can see.

Good point. The correct answer, is that "there *may* exist some type,
but its implementation defined (if it exists at all)".
 

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
474,202
Messages
2,571,057
Members
47,660
Latest member
vidotip479

Latest Threads

Top