473,421 Members | 1,627 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

size_t and ptr_diff_t

I have the idea that ptr_diff_t had to be the same size as size_t from
Plauger's "The Standard C Library," where he states "... It is always
the signed type that has the same number of bits as the4 unsigned type
chosen for size_t..." This language would not rule out one being int
and the other long so long as sizeof(int)==sizeof(long) for the
implementation.

Now I can't see anywhere in the standard that would require that, at
least not directly, and it seems that a size_t of unsigned int and a
prtdiff_t of long (where int and long are different sizes) would be
possible. C99 defines SIZE_MAX as being at least 65535, and
PTRDIFF_MIN/MAX as being at least -/+65535.

So do size_t and ptr_diff_t have to be the same size (or base type) or
not?

Aug 23 '07 #1
9 4123

"Bob Cassel" <bc@DELETEthis.comwrote in message
news:sl********************@nospam.invalid...
>I have the idea that ptr_diff_t had to be the same size as size_t from
Plauger's "The Standard C Library," where he states "... It is always
the signed type that has the same number of bits as the4 unsigned type
chosen for size_t..." This language would not rule out one being int
and the other long so long as sizeof(int)==sizeof(long) for the
implementation.

Now I can't see anywhere in the standard that would require that, at
least not directly, and it seems that a size_t of unsigned int and a
prtdiff_t of long (where int and long are different sizes) would be
possible. C99 defines SIZE_MAX as being at least 65535, and
PTRDIFF_MIN/MAX as being at least -/+65535.

So do size_t and ptr_diff_t have to be the same size (or base type) or
not?
The problem is that size_t is usally the width of the address bus, because
the biggest object you can hold in memory is all of memory.
ptrdiff_t has to represent the difference between two pointers, so it must
be signed. Therefore it needs an extra bit.
However that is probably inefficient. Most pointers are not character
pointers anyway. In practise the promise for subtracting very distant
character pointers is not honoured, and ptrdiff_t is a signed type with the
same number of bits as size_t.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 23 '07 #2
Malcolm McLean wrote, On 23/08/07 21:41:
>
"Bob Cassel" <bc@DELETEthis.comwrote in message
news:sl********************@nospam.invalid...
>I have the idea that ptr_diff_t had to be the same size as size_t from
Plauger's "The Standard C Library," where he states "... It is always
the signed type that has the same number of bits as the4 unsigned type
chosen for size_t..." This language would not rule out one being int
and the other long so long as sizeof(int)==sizeof(long) for the
implementation.

Now I can't see anywhere in the standard that would require that, at
least not directly, and it seems that a size_t of unsigned int and a
prtdiff_t of long (where int and long are different sizes) would be
possible. C99 defines SIZE_MAX as being at least 65535, and
PTRDIFF_MIN/MAX as being at least -/+65535.

So do size_t and ptr_diff_t have to be the same size (or base type) or
not?
They are not required to be the same size. In fact, with the minimum
requirements you specify (which are correct) it would make sense on a 16
bit system to have size_t as a 16 bit unsigned integer (unsigned int
probably) and ptrdiff_t a signed 32 bit integer (long probably). The
same sort of thing applies to a 64 bit system which limits the maximum
size of a single object to (2^32)-1, which would not be unreasonable.
The problem is that size_t is usally the width of the address bus,
because the biggest object you can hold in memory is all of memory.
Historically some systems have limited the largest single object to be
much smaller than the address bus allows. For example only allowing
objects up to 64K on an 80286 or 68000 for efficiency reasons.
ptrdiff_t has to represent the difference between two pointers, so it
must be signed. Therefore it needs an extra bit.
However that is probably inefficient. Most pointers are not character
pointers anyway. In practise the promise for subtracting very distant
character pointers is not honoured, and ptrdiff_t is a signed type with
the same number of bits as size_t.
True. In practical terms I would be very surprised if anyone allocated a
single object as large as half the size the address bus can allocate, so
a ptrdiff_t type the same width as the address bus should be fine.
--
Flash Gordon
Aug 23 '07 #3
On Aug 23, 9:41 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
"Bob Cassel" <b...@DELETEthis.comwrote in message

news:sl********************@nospam.invalid...
I have the idea that ptr_diff_t had to be the same size as size_t from
Plauger's "The Standard C Library," where he states "... It is always
the signed type that has the same number of bits as the4 unsigned type
chosen for size_t..." This language would not rule out one being int
and the other long so long as sizeof(int)==sizeof(long) for the
implementation.
Now I can't see anywhere in the standard that would require that, at
least not directly, and it seems that a size_t of unsigned int and a
prtdiff_t of long (where int and long are different sizes) would be
possible. C99 defines SIZE_MAX as being at least 65535, and
PTRDIFF_MIN/MAX as being at least -/+65535.
So do size_t and ptr_diff_t have to be the same size (or base type) or
not?

The problem is that size_t is usally the width of the address bus, because
the biggest object you can hold in memory is all of memory.
ptrdiff_t has to represent the difference between two pointers, so it must
be signed. Therefore it needs an extra bit.
However that is probably inefficient. Most pointers are not character
pointers anyway. In practise the promise for subtracting very distant
character pointers is not honoured, and ptrdiff_t is a signed type with the
same number of bits as size_t.
I think it is highly unlikely that size_t is in any way related to the
width of the address bus. For example, I can tell my compiler to
generate 32 bit code or 64 bit code, and size_t is different
(actually, I think it is a 32 bit unsigned long in one case and a 64
bit unsigned long in the other case), but the width of the address bus
is exactly the same.

Aug 23 '07 #4
"Malcolm McLean" <re*******@btinternet.comwrites:
"Bob Cassel" <bc@DELETEthis.comwrote in message
news:sl********************@nospam.invalid...
>>I have the idea that ptr_diff_t had to be the same size as size_t from
Plauger's "The Standard C Library," where he states "... It is always
the signed type that has the same number of bits as the4 unsigned type
chosen for size_t..." This language would not rule out one being int
and the other long so long as sizeof(int)==sizeof(long) for the
implementation.

Now I can't see anywhere in the standard that would require that, at
least not directly, and it seems that a size_t of unsigned int and a
prtdiff_t of long (where int and long are different sizes) would be
possible. C99 defines SIZE_MAX as being at least 65535, and
PTRDIFF_MIN/MAX as being at least -/+65535.

So do size_t and ptr_diff_t have to be the same size (or base type) or
not?
The problem is that size_t is usally the width of the address bus,
because the biggest object you can hold in memory is all of memory.
That's commonly true, but the standard doesn't require it, and there
have been systems where the maximum size of an object is less than all
of memory. Note that both size_t and ptrdiff_t (note the spelling)
are meaningful only for single objects. size_t is the result of
sizeof, which can only be applied to a single object; ptrdiff_t is the
result of pointer subtraction, which invokes undefined behavior unless
both pointers point into the same object.
ptrdiff_t has to represent the difference between two pointers, so it
must be signed. Therefore it needs an extra bit.
However that is probably inefficient. Most pointers are not character
pointers anyway. In practise the promise for subtracting very distant
character pointers is not honoured, and ptrdiff_t is a signed type
with the same number of bits as size_t.
True. There's no explicit requirement in the standard for size_t and
ptrdiff_t to be the same size, but they commonly are (I've never heard
of a system where they aren't). It could make sense to make ptrdiff_t
larger because it "wastes" one bit for the sign, but that's rarely
done, if ever.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 23 '07 #5
Malcolm McLean wrote On 08/23/07 16:41,:
>
The problem is that size_t is usally the width of the address bus
[...]
Why do you keep on making this claim after it's been
refuted over and over again?

The machine in front of me at this moment has a 34-bit
address bus (it's a creaky old machine; the shoemaker's
children go barefoot). Depending on the compiler options,
size_t is either 32 or 64 bits wide.

--
Er*********@sun.com
Aug 23 '07 #6
On Aug 24, 9:54 am, Eric Sosman <Eric.Sos...@sun.comwrote:
Malcolm McLean wrote On 08/23/07 16:41,:
[...]

Why do you keep on making this claim after it's been
refuted over and over again?
Because that's what trolls do?

Aug 23 '07 #7
Malcolm McLean wrote:
In practise the promise for subtracting very distant
character pointers is not honoured,
and ptrdiff_t is a signed type with the
same number of bits as size_t.
There's no promise,
unless a program stays inside the minimum environmental limits,
and then the promise is kept.

N869

5.2.4.1 Translation limits
-- 65535 bytes in an object (in a hosted environment only)
7.18.3 Limits of other integer types
-- limits of ptrdiff_t
PTRDIFF_MIN -65535
PTRDIFF_MAX +65535
6.5.6 Additive operators
[#9] When two pointers are subtracted, both shall point to
elements of the same array object, or one past the last
element of the array object; the result is the difference of
the subscripts of the two array elements. The size of the
result is implementation-defined, and its type (a signed
integer type) is ptrdiff_t defined in the <stddef.hheader.
If the result is not representable in an object of that
type, the behavior is undefined.

--
pete
Aug 23 '07 #8
On Thu, 23 Aug 2007 14:11:42 -0700, "christian.bau"
<ch***********@cbau.wanadoo.co.ukwrote in comp.lang.c:
On Aug 23, 9:41 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
"Bob Cassel" <b...@DELETEthis.comwrote in message

news:sl********************@nospam.invalid...
>I have the idea that ptr_diff_t had to be the same size as size_t from
Plauger's "The Standard C Library," where he states "... It is always
the signed type that has the same number of bits as the4 unsigned type
chosen for size_t..." This language would not rule out one being int
and the other long so long as sizeof(int)==sizeof(long) for the
implementation.
Now I can't see anywhere in the standard that would require that, at
least not directly, and it seems that a size_t of unsigned int and a
prtdiff_t of long (where int and long are different sizes) would be
possible. C99 defines SIZE_MAX as being at least 65535, and
PTRDIFF_MIN/MAX as being at least -/+65535.
So do size_t and ptr_diff_t have to be the same size (or base type) or
not?
The problem is that size_t is usally the width of the address bus, because
the biggest object you can hold in memory is all of memory.
ptrdiff_t has to represent the difference between two pointers, so it must
be signed. Therefore it needs an extra bit.
However that is probably inefficient. Most pointers are not character
pointers anyway. In practise the promise for subtracting very distant
character pointers is not honoured, and ptrdiff_t is a signed type with the
same number of bits as size_t.

I think it is highly unlikely that size_t is in any way related to the
width of the address bus. For example, I can tell my compiler to
generate 32 bit code or 64 bit code, and size_t is different
(actually, I think it is a 32 bit unsigned long in one case and a 64
bit unsigned long in the other case), but the width of the address bus
is exactly the same.
As much as I dislike the idea of standing up for Malcolm, I think his
meaning is quite logical even if his use of the term "address bus" is
ill-advised. If you replace that by saying:

"size_t is usually the smallest unsigned integer type that can hold
all the bits in a pointer to char", it makes more sense.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Aug 24 '07 #9

"Eric Sosman" <Er*********@sun.comwrote in message
news:1187906046.696445@news1nwk...
Malcolm McLean wrote On 08/23/07 16:41,:
>>
The problem is that size_t is usally the width of the address bus
[...]

Why do you keep on making this claim after it's been
refuted over and over again?

The machine in front of me at this moment has a 34-bit
address bus (it's a creaky old machine; the shoemaker's
children go barefoot). Depending on the compiler options,
size_t is either 32 or 64 bits wide.
Usually.
You can of course implement a compiler with 42 bit pointers on top of a
32-bit architecture, and have correspondingly weird rules for size_t.
However it is not normal to do that sort of thing.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 24 '07 #10

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

Similar topics

6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
11
by: javadesigner | last post by:
Hi: I am a bit new to C programming and am trying to write a wrapper around malloc. Since malloc takes size_t as it's parameter, I want to be able to see if my function recieved an argument...
16
by: Xenos | last post by:
Is there a standard way to determine the max. value of size_t as a compile-time constant? Will: #define SIZE_T_MAX ((size_t) -1) work in all cases, or just on 2s comp. machines? DrX
5
by: edware | last post by:
Hello, I have some questions about the size_t type. First, what do we know about size_t? From what I have read I believe that it is an unsigned integer, but not necessarily an int. Am I correct?...
12
by: Alex Vinokur | last post by:
Why was the size_t type defined in compilers in addition to unsigned int/long? When/why should one use size_t? Alex Vinokur email: alex DOT vinokur AT gmail DOT com...
39
by: Mark Odell | last post by:
I've always declared variables used as indexes into arrays to be of type 'size_t'. I have had it brought to my attention, recently, that size_t is used to indicate "a count of bytes" and that using...
73
by: Yevgen Muntyan | last post by:
Hey, I was reading C99 Rationale, and it has the following two QUIET CHANGE paragraphs: 6.5.3.4: "With the introduction of the long long and extended integer types, the sizeof operator may...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
27
by: mike3 | last post by:
Hi. I can't believe I may have to use an array here. I've got this bignum package I was making in C++ for a fractal generator, and tried an approach that was suggested to me here a while...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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
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...

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.