Size of pointer to opaque object

E

Ellarco

``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 of the documentation on it.

The above means that objectID is defined as a pointer to an as yet undefined
class/struct _objID, and, if I understand the term correctly, is opaque in
so far as the actual nature of the _objID class/struct is as yet unknown,
yeah?

My problem is that I need to statically determine the size of an objectID. I
need to now that it occupies the same amount of space as 1,2,3... chars, but
without using sizeof(objectID). Is it possible? I was thinking that it might
be the same size as char* or something convenient like that.

TIA
 
V

Victor Bazarov

Ellarco said:
``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 of the documentation on it.

The above means that objectID is defined as a pointer to an as yet undefined
class/struct _objID, and, if I understand the term correctly, is opaque in
so far as the actual nature of the _objID class/struct is as yet unknown,
yeah?

My problem is that I need to statically determine the size of an objectID. I
need to now that it occupies the same amount of space as 1,2,3... chars, but
without using sizeof(objectID). Is it possible? I was thinking that it might
be the same size as char* or something convenient like that.

The size of a pointer to any object is the same and is the same as
a pointer to void. So, you may use sizeof(void*) for your purposes.

Victor
 
G

Gianni Mariani

Ellarco wrote:
....
My problem is that I need to statically determine the size of an objectID. I
need to now that it occupies the same amount of space as 1,2,3... chars, but
without using sizeof(objectID). Is it possible? I was thinking that it might
be the same size as char* or something convenient like that.

What is stopping you from using sizeof(objectID) ?
 
A

Alexander Terekhov

Victor Bazarov wrote:
[...]
Yes. Use sizeof(_objID *).
The size of a pointer to any object is the same and is the same as
a pointer to void.

Change the clinic, Bazarov.

regards,
alexander.
 
R

Ron Natalie

Victor Bazarov said:
The size of a pointer to any object is the same and is the same as
a pointer to void. So, you may use sizeof(void*) for your purposes.
This is NOT true. A void* is required to hold a converted poitner value to
any object and have the same representation as char*, but it is not necessary
for it to be the same size as all the other pointers.

What is true however, is that the sizeof the pointer to any class has to be
the same size. If this were not true, than creating objects of type pointer
to incomplete classes, wouldn't be possible as the size would not be known.
However, this size can be smaller than sizeof(void*).
 
W

WW

Alexander Terekhov wrote:
[SNIP]
Change the clinic, Bazarov.

Change the "cinic", Terekhov.

BTW it can make a whole lot of improvement in communication if you also say
want the point is, not only the insult part. :)
 
A

Alexander Terekhov

WW wrote:
[...]
BTW it can make a whole lot of improvement in communication if you also say
want the point is, not only the insult part. :)

What would Ron do here, then?
--
Whitov Wolfov aka Attilov Feherov

Ps: Hota moja ckraju netchevo ne znaju (try to dechipher it, I'va heard it
from my father)

That was probably in reply to your mother asking (with some "elements"
of shouting) your father "Where's The Money Gone?" (or something like
that), I guess. ;-)
 
W

WW

Alexander said:
That was probably in reply to your mother asking (with some "elements"
of shouting) your father "Where's The Money Gone?" (or something like
that), I guess. ;-)

No, actually I was supposed to tell it to the Baltic state(s) borderguards
if they keep asking too many questions. :)
 
W

Wagner Bruna

Hello,


Just say 'sizeof (objectID)'.

To know the size of any object, you need the definition of its type.
But if you just want to know the pointer's size, you only need a
forward declaration; it works even with pointers to incomplete types.

(...)
The size of a pointer to any object is the same and is the same as
a pointer to void.

This is true in most platforms, but actually it's implementation
dependent.


++t;
Wagner
 
G

glen stark

Wagner said:
Hello,




This is true in most platforms, but actually it's implementation
dependent.

This is an interesting assertion, and makes me think I don't unserstand
pointers correctly? I thought a pointer contained a memory address, and
that memory addresses had the same size regardless of what that address
is... Otherwise how would it be kosher for me to declare pointers to
undefined Classes. And since most any pointer can be cast to a pointer
to void, a pointer to void would have to be the same size.

Can you give me a counter example where the statement 'the size of a
pointer to any object is the same and [its size] is the same as a
pointer to void' does not hold true?

glen stark
 
K

Karl Heinz Buchegger

glen said:
This is an interesting assertion, and makes me think I don't unserstand
pointers correctly? I thought a pointer contained a memory address, and
that memory addresses had the same size regardless of what that address
is...

One needs to distuingish between objects of class type and objects
of builtin type. A character pointer may indeed have a different
size then a pointer to a class object.
Otherwise how would it be kosher for me to declare pointers to
undefined Classes.

pointers to class object have the same size. Although it
is not mentioned explicitely in the standard, it follows
from eg. what you say (forward declarations would not work).
And since most any pointer can be cast to a pointer
to void, a pointer to void would have to be the same size.

A void pointer could be larger then any other pointer. The only
requirement is, that any pointer can be cast to void* and back
without loosing information.
Can you give me a counter example where the statement 'the size of a
pointer to any object is the same and [its size] is the same as a
pointer to void' does not hold true?

Well. The standard does not require this. But AFAIK there have indeed
been machines where character pointers where treated differently due
to alignment restrictions on that CPU. Eg. if you know that an int
must always be stored at an address divisible by 4, then you know that
the last 2 bits of that address are always 0. So instead of storing
those 2 bits, you can shift the whole address 2 times to the right
and still haven't lost any information (but gained a larger address
room). But the same may not work for characters where you want to
address each and every byte.
 
R

Ron Natalie

glen stark said:
This is an interesting assertion, and makes me think I don't unserstand
pointers correctly? I thought a pointer contained a memory address, and
that memory addresses had the same size regardless of what that address
is...

Your understanding is incomplete. A pointer is not a memory address. A pointer
is a data type that refers to a location in memory. The pointer is frequently
some hardware supported memory or register format. Not all machines just use
a 32-bit byte addressed value. Some machines have word based pointer types
and would to have additional information for addressing partial words which may
make the pointers to those types larger. The language forsees this. No where
in the document does it say pointers are the same size or even the same layout
with the exception that void* and char* are the same.
Otherwise how would it be kosher for me to declare pointers to
undefined Classes. And since most any pointer can be cast to a pointer
to void, a pointer to void would have to be the same size.

That's not true, a void pointer just has to be able to store a converted value
from any other pointer. The reverse is not true.
char x;
char* cp = (char*)(int*)(char*) &x;
The char* to int* conversion may lose information resulting in an invalid
conversion.
 
J

Jerry Coffin

[ ... ]
This is an interesting assertion, and makes me think I don't unserstand
pointers correctly? I thought a pointer contained a memory address, and
that memory addresses had the same size regardless of what that address
is...

Maybe, or maybe not. First of all, it's necessary to distinguish
between pointers to data and pointers to functions. Pointers to data
come in two varieties: pointers to global functions are one sort of
things, and all of them are compatible with each other. Pointers to
member functions are entirely different (and only rarely useful, so I
won't got into further detail about them for the moment).

Pointers to data have a few requirements: a pointer to void has the same
representation as a pointer to char, and any other kind of pointer can
be converted to a pointer to void and back to its original type without
losing information. Though not explicitly stated, the fact that a
pointer to char has the same representation as a pointer to void seems
to imply that you should be able to do the same thing (convert pointer
to some other type to pointer to char and back without losing anything).

Other than that, most bets are off -- different sorts of pointers could
have different sizes and representations. For one example, consider a
64-bit machine with word-addressing, but not byte-addressing. A pointer
to char on such a machine might consist of the address of a word AND the
number of a byte within that word.

Another possibility would be a machine that provided hardware support
for quite a bit of memory protection -- a pointer on such a machine
might include not only the address of the object, but also information
about things like the greatest legal offset from that address, so you
can't accidentally dereference a pointer outside its bounds.

Both of these (roughly) represent real machines -- Crays are similar to
the first, and AS/400's the second.
Otherwise how would it be kosher for me to declare pointers to
undefined Classes.

This can be handled a number of ways -- for one example, there might be
an optimized representation for a pointer under some circumstances, and
a less optimal version that will work under any circumstances. In this
case, a pointer based on a forward declaration would work, even though
it might be less efficient than would otherwise be the case.
And since most any pointer can be cast to a pointer
to void, a pointer to void would have to be the same size.

Not so -- a pointer to void basically has to be AT LEAST as large as any
other (i.e. has to be able to hold all the information in any other) but
could contain MORE information than a pointer to some other type.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top