473,506 Members | 17,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q's on pointer sizes

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
Jul 26 '06 #1
5 1292
Gonçalo Rodrigues wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '06 #2
Gonçalo Rodrigues wrote:
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.
Jul 26 '06 #3
Gonçalo Rodrigues wrote:
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.

--
Best regards,
Andrey Tarasevich
Jul 26 '06 #4
red floyd wrote:
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

Jul 26 '06 #5
Kai-Uwe Bux wrote:
red floyd wrote:
>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)".
Jul 26 '06 #6

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

Similar topics

12
4701
by: Ellarco | last post by:
``Opaque-pointer representing the ID of an object. struct _objID; typedef struct _objID * objectID;'' Hi again. Im using an api that defines an objectID type. The above represents the extent...
6
1812
by: Jackie | last post by:
Does anyone know how to get a count for a pointer to an array for example double** someFnct () { cout <<"Enter num of row: " <<endl; cin >> n; cout <<"Enter num of column: " <<endl;
7
3095
by: Fabian Wauthier | last post by:
Hi list, I am trying to dynamically grow a 2 dimensional array (Atom ***Screen) of pointers to a struct Atom (i.e. the head of a linked list). I am not sure if this is the right way to do it: ...
16
2266
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
33
17499
by: siliconwafer | last post by:
What is size of pointer in C on DOS? is it sizeof(int ) or size of (long int)? If this ans is present in FAQ pls direct me to tht ouestion
48
2117
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
49
2737
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
3
2241
by: josh | last post by:
Why if I use: Array *Array::operator=( const Array *right ) { if ( right != this ) { // check for self-assignment // for arrays of different sizes, deallocate original // left side array,...
34
1857
by: sumedh..... | last post by:
double * X size of X->?? size of X->?? double (*X) size of X->?? size of X->??
1
7286
by: ladesidude | last post by:
Hi, I have this program in C, and have commented each line as per my understanding. At the end, I have some questions which I havent been able to understand. Any help would be appreciated. ...
0
7220
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
7308
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,...
0
7371
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...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.