468,554 Members | 1,901 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,554 developers. It's quick & easy.

Size of pointer to opaque object

``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

Jul 19 '05 #1
12 4354
"Ellarco" <no****@eircom.net> wrote...
``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
Jul 19 '05 #2
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) ?

Jul 19 '05 #3

Victor Bazarov 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?
Yes. Use sizeof(_objID *).
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.


Change the clinic, Bazarov.

regards,
alexander.
Jul 19 '05 #4

"Victor Bazarov" <v.********@attAbi.com> wrote in message news:L5*******************@rwcrnsc51.ops.asp.att.n et...
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*).
Jul 19 '05 #5
WW
Alexander Terekhov wrote:
[SNIP]
The size of a pointer to any object is the same and is the same as
a pointer to void.


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. :-)

--
Whitov Wolfov aka Attilov Feherov

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

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. ;-)
Jul 19 '05 #7
WW
Alexander Terekhov wrote:
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. ;-)


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

--
WW aka Attila
Jul 19 '05 #8
Hello,

"Victor Bazarov" <v.********@attAbi.com> wrote in message news:<L5*******************@rwcrnsc51.ops.asp.att. net>...
"Ellarco" <no****@eircom.net> wrote...
``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.

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
Jul 19 '05 #9
Wagner Bruna wrote:
Hello,

"Victor Bazarov" <v.********@attAbi.com> wrote in message news:<L5*******************@rwcrnsc51.ops.asp.att. net>...
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.


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

Jul 19 '05 #10


glen stark wrote:

Wagner Bruna wrote:
Hello,

"Victor Bazarov" <v.********@attAbi.com> wrote in message news:<L5*******************@rwcrnsc51.ops.asp.att. net>...
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.


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #11

"glen stark" <ma**@glenstark.org.nospam> wrote in message news:3f********@pfaff2.ethz.ch...

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.
Jul 19 '05 #12
In article <3f********@pfaff2.ethz.ch>, ma**@glenstark.org.nospam
says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by David Gilbert | last post: by
18 posts views Thread by Tarundeep | last post: by
7 posts views Thread by Alfonso Morra | last post: by
18 posts views Thread by Anand Buddhdev | last post: by
5 posts views Thread by Danilo Kempf | last post: by
11 posts views Thread by quakewang | last post: by
4 posts views Thread by Kenneth Brody | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.