When should I use ptrdiff_t and size_t instead of int and unsigned int?

P

PengYu.UT

I'm wondering when I should use ptrdiff_t and size_t instead of int and
unsigned int or long and unsigned long? Is there any guideline I should
follow?

Peng
 
V

Victor Bazarov

I'm wondering when I should use ptrdiff_t and size_t instead of int
and unsigned int or long and unsigned long? Is there any guideline I
should follow?

When you subtract two pointers, the result is 'ptrdiff_t'. 'size_t' is
the result of 'sizeof', and it is passed to 'new'. Other than that, use
'unsigned' as you wish. Generally, those types are interchangeable,
but in some cases they are not, and then your compiler will probably
warn you if you try to assign 'ptrdiff_t' to 'unsigned', for example.
IIRC, in Win64, 'ptrdiff_t' is a 64-bit type, while both 'unsigned' and
'unsigned long' are 32 bits.

V
 
P

PengYu.UT

What type should I use as the index of an array? Because the index is
always non-negtive, so ptrdiff_t should not be used, right? Because
size_t is used to allocate memory, when I index an array, it is better
to use size_t rather than unsigned. Right?

Peng
 
J

Jack Klein

I'm wondering when I should use ptrdiff_t and size_t instead of int and
unsigned int or long and unsigned long? Is there any guideline I should
follow?

Peng

Yes, just use them for what they are intended for.

ptrdiff_t is a signed integer type which holds the difference between
two pointers. So use it when you want to represent the difference
between two pointers, when it is legal to do so.

size_t is an unsigned integer type which represents the size of an
object in bytes. It is the type produced by the sizeof operator and
is used as a parameter or return value of some library functions.

I imagine that there are 64 bit implementations right now where
ptrdiff_t and size_t are 64 bits, but signed and unsigned long are
only 32 bits. C++ does not yet have an integer type required to have
at least 64 bits.
 
V

Victor Bazarov

What type should I use as the index of an array? Because the index is
always non-negtive,

Why do you say that? Always non-negative?

int a[10] = {0}, *p3 = a + 3, &r = p[-1];
so ptrdiff_t should not be used, right?

Use any integral expression you want.
Because
size_t is used to allocate memory, when I index an array, it is better
to use size_t rather than unsigned. Right?

No. Indexing is defined when an integral expression is added to,
or subtracted from, a pointer.

V
 
G

Greg Comeau

What type should I use as the index of an array? Because the index is
always non-negtive, so ptrdiff_t should not be used, right?
Right.

Because
size_t is used to allocate memory, when I index an array, it is better
to use size_t rather than unsigned. Right?

Technically right. I say technically because there have
been "systems" where size_t was not able to be properly
metabolized for it, do to the way memory was set up, etc.
So while I may have worked for malloc or new, it wasn't
able to properly capture all sizes, and hence say in
those cases a larger unsigned may have also been permissable
(size_t is an unsigned already, but which one is implementation
defined).
 
G

Greg Comeau

Technically right. I say technically because there have
been "systems" where size_t was not able to be properly
metabolized for it, do to the way memory was set up, etc.
So while I may have worked for malloc or new, it wasn't
able to properly capture all sizes, and hence say in
those cases a larger unsigned may have also been permissable
(size_t is an unsigned already, but which one is implementation
defined).

BTW, responding to myself:

* Above when you say index, I'm assuming you mean the dimension
of the array, that is it's bounds.

* Also when I say you are right that ptrdiff_t should not be
used, I mean this more as it probably shouldn't be your first
choice, not that it would work in some cases.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top