473,804 Members | 3,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4570
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*******@gmai l.com"
<Pe*******@gmai l.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.l earn.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************ *********@g47g2 000cwa.googlegr oups.com>,
Pe*******@gmail .com <Pe*******@gmai l.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**********@p anix3.panix.com >,
Greg Comeau <co****@comeauc omputing.com> wrote:
In article <11************ *********@g47g2 000cwa.googlegr oups.com>,
Pe*******@gmai l.com <Pe*******@gmai l.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
3099
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. I'm wondering why ::size_t and ::ptrdiff_t aren't undefined. Thanks, Peng
49
5912
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. Effectively, though, signed integers would often work too since unless I am doing some modular arithmetic modulo the word length then I almost never need to use the high bit since the integers I deal with are usually not that large, and I would assume...
16
6149
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
6722
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 already defined as such. I got the idea that ptrdiff_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...
12
10727
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 http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
27
2312
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 this array as an argument, is there a safe way to establish how many rows that are or should I iterate over a field I know will always be used and use the final value of the iterator as the value of the array?
10
3504
by: kyagrd | last post by:
<code> #include <iostream> int main(void) { using namespace std; int p; int* p1 = p; int* p11 = p + 2;
73
7459
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 yield a value that exceeds the range of an unsigned long." 6.5.6: "With the introduction of the long long and extended integer
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
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
10568
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
10311
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
10074
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5516
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
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
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

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.