473,395 Members | 1,368 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,395 software developers and data experts.

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

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

Oct 19 '05 #1
6 4541
Pe*******@gmail.com wrote:
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
Oct 19 '05 #2
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

Oct 19 '05 #3
On 18 Oct 2005 19:13:21 -0700, "Pe*******@gmail.com"
<Pe*******@gmail.com> wrote in comp.lang.c++:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 19 '05 #4
Pe*******@gmail.com wrote:
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
Oct 19 '05 #5
In article <11*********************@g47g2000cwa.googlegroups. com>,
Pe*******@gmail.com <Pe*******@gmail.com> wrote:
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).
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 19 '05 #6
In article <dj**********@panix3.panix.com>,
Greg Comeau <co****@comeaucomputing.com> wrote:
In article <11*********************@g47g2000cwa.googlegroups. com>,
Pe*******@gmail.com <Pe*******@gmail.com> wrote:
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).


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.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 19 '05 #7

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

Similar topics

9
by: PengYu.UT | last post by:
std::size_t and std::ptrdiff_t are defined in <cstddef>. I've noticed that a bunch of old math function in the global namespace are undefined in <cmath> and they are redefined in std namespace. ...
49
by: Neil Zanella | last post by:
Hello, Often I happen to be dealing with nonnegative integers and since I know I won't need negative numbers here I declare them as unsigned simply to make the program somewhat clearer....
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
26
by: robertwessel2 | last post by:
In another thread, a poster mentioned the Posix ssize_t definition (signed version of size_t). My initial reaction was to wonder what the point of the Posix definition was when ptrdiff_t was...
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...
27
by: pkirk25 | last post by:
Assume an array of structs that is having rows added at random. By the time it reaches your function, you have no idea if it has a few hundred over over 10000 rows. When your function recieves...
10
by: kyagrd | last post by:
<code> #include <iostream> int main(void) { using namespace std; int p; int* p1 = p; int* p11 = p + 2;
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.