Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sizeof (size_t) and sizeof (pointer)

Reply
Thread Tools

sizeof (size_t) and sizeof (pointer)

 
 
Alex Vinokur
Guest
Posts: n/a
 
      11-12-2007
Does it have to be? :
sizeof (size_t) >= sizeof (pointer)

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      11-12-2007
Alex Vinokur wrote:
> Does it have to be? :
> sizeof (size_t) >= sizeof (pointer)
>

No. size_t only has to be big enough to represent the
maximum number of objects that could be created. There
are implementations where the sizeof the pointer is bigger
than even the number of chars that could be allocated (i.e,
not all the bits in the pointer were used to contribute
tot he address). It's also not the case that all pointers
need to be the same size.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-12-2007
* Alex Vinokur:
> Does it have to be? :
> sizeof (size_t) >= sizeof (pointer)


No.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      11-12-2007
Ron Natalie wrote:
> It's also not the case that all pointers
> need to be the same size.


Is that really so? I thought that it must be possible to cast any
pointer to and from a void*. If there were different-sized pointers then
it could be rather problematic.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-12-2007
* Juha Nieminen:
> Ron Natalie wrote:
>> It's also not the case that all pointers
>> need to be the same size.

>
> Is that really so? I thought that it must be possible to cast any
> pointer to and from a void*. If there were different-sized pointers then
> it could be rather problematic.


There are different size pointers, e.g. member pointers tend to be
larger than others. Then there are function pointers in general, which
for freestanding functions tend to be the same size as data pointers,
but cannot be cast to void*. And that's intentionally "problematic".

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-12-2007
Juha Nieminen wrote:
> Ron Natalie wrote:
>> It's also not the case that all pointers
>> need to be the same size.

>
> Is that really so? I thought that it must be possible to cast any
> pointer to and from a void*. If there were different-sized pointers
> then it could be rather problematic.


What if void* is at least as large as the largest of them? If the
sizes do differ, it would makes sense, no?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      11-12-2007
Juha Nieminen wrote:
:: Ron Natalie wrote:
::: It's also not the case that all pointers
::: need to be the same size.
::
:: Is that really so? I thought that it must be possible to cast any
:: pointer to and from a void*. If there were different-sized
:: pointers then it could be rather problematic.

You can only reliably cast the pointer back to the original type. So,
as long as void* is among the largest types, other pointers can be
smaller.



Bo Persson


 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-13-2007
Alex Vinokur wrote:
> Does it have to be? :
> sizeof (size_t) >= sizeof (pointer)
> ...


Firstly, by 'pointer' you probably mean something like 'void*', since,
say, an object of 'SomeType (SomeClass::*)()' is also a "pointer" (a
pointer-to-member-function) and it most likely will be [a lot] bigger
that a 'size_t' on the same implementation.

Secondly, even the ordinary 'void*' can be bigger than 'size_t'. In
fact, typical DOS/Win16 implementations used to have a 16-bit 'size_t'
and 32-bit 'void*' pointers (depended on memory model).

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-13-2007
Juha Nieminen wrote:
>> It's also not the case that all pointers
>> need to be the same size.

>
> Is that really so? I thought that it must be possible to cast any
> pointer to and from a void*. If there were different-sized pointers then
> it could be rather problematic.


Well, to be C++-pedantic, you can expect to cast literally _any_ pointer
to 'void*' that way. Only pointers to object types can be cast to
'void*' and back. Having noted that, the round-trip conversion 'T*' ->
'void*' -> 'T*' is indeed guaranteed to preserve the original value of
'T*' pointer, but that only means that value representation of 'void*'
is at least as "precise" (as big) as the value representation of any
'T*' type. Yet various 'T*' can still have different representations
(including different sizes).

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-13-2007
Ron Natalie wrote:
> size_t only has to be big enough to represent the
> maximum number of objects that could be created.


Hmm... By definition, size_t has to be big enough to represent the
number of bytes in a single object. If you prefer to express it in terms
of "number of objects", it should probably sound like "size_t only has
to be big enough to represent the maximum number of [continuous] bytes
that could be allocated for a single object". Although I don't see the
point in trying to reformulate it like that.

To say that it should represent "maximum number of objects that could be
created" is misleading. Quite the opposite, in general case there's
nothing that prevents one from creating more objects than size_t can count.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) == 1?) Alex Vinokur C Programming 7 08-14-2006 04:57 PM
sizeof( int ) != sizeof( void * ) blufox C Programming 2 05-22-2006 03:25 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57