473,657 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof (size_t) and sizeof (pointer)

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

Nov 12 '07 #1
16 15646
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.
Nov 12 '07 #2
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.
Nov 12 '07 #3
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
Nov 12 '07 #4
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
Nov 12 '07 #5
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
Nov 13 '07 #6
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
Nov 13 '07 #7
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
Nov 13 '07 #8
On Nov 12, 6:39 pm, Juha Nieminen <nos...@thanks. invalidwrote:
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.
It is, and your assumption is wrong. You can cast any pointer
to an object to void*, but all you are guaranteed then is that
you can cast it back to the original type without loss of
information. There are also guarantees concerning accessing
objects as arrays of char or unsigned char. All of which,
together, more or less imply that sizeof(void*) == sizeof(char*)
(an explicit requirement in the C standard), and that
sizeof(void*) >= sizeof(T*) for all object types T. I've worked
on systems where char* was larger than int*, and of course,
systems where void (*)() had a different size than char* were
quite frequent once upon a time---you can still find their
descendants in use today. (The same systems often had a size_t
which was smaller than a data pointer. And in some cases, data
pointers or function pointers which were larger than any
integral type.)

You cannot, of course, convert a pointer to function, or any
ponter to member, to a void*; an attempt to do so is an error,
and requires a diagnositic.

Some standards place stricter requirements on the
implementation: Posix does require object pointers and function
pointers have the same size and representation, for example.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 13 '07 #9
On Nov 12, 1:51 pm, Alex Vinokur <ale...@users.s ourceforge.netw rote:
Does it have to be? :
sizeof (size_t) >= sizeof (pointer)
Formally, there's no relation. Both can be pretty much anything
the implementation wants. Practically, you have the
relationship reversed: I can't think of a reason why
sizeof(size_t) <= sizeof(char*) would ever hold. (Of course, if
by "pointer", you mean any pointer, and not just a pointer to an
object, anything goes. I've used systems where the size of a
function pointer was two bytes, but size_t and data pointers
were four bytes.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 13 '07 #10

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

Similar topics

13
7675
by: Vinu | last post by:
The following code doesn't prints anything why it is? The code is correct. plz explain the logic #include <stdio.h> int arr = {10,20,30,40,50}; #define ARR_SIZE sizeof(arr)/sizeof(arr) void main()
2
3654
by: blufox | last post by:
I read somewhere that standard Ansi C on 32 bit architecures assumes a lot of things which lead to poor code. For example sizeof( int ) = sizeof( void * ) sizeof( long ) = sizeof( int ) Why is this poor code? Am i missing something?
10
3010
by: Kislay | last post by:
int main() { int i=10; printf("\n Size of i = %d ",sizeof(++i)); printf("\n i = %d ",i); system("pause"); return 0; } On executing the above code , the value of i obtained as 10 . What
20
2160
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then suddenly I can't use sizeof(array) / sizeof(array) anymore within that function ? Help - What point am I missing ?
8
1960
by: Bill Cunningham | last post by:
I would like to check the sizeof size_t. Anyone have a suggestion? Bill
14
2784
by: ManicQin | last post by:
Hi all. I'm trying to get the size of a variable in a struct by his relative postion i.e. /// #define offsetof(s,m) (size_t)&(((s *)0)->m) struct ThePimp{ char rings; char blings;
0
8306
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7327
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.