﻿# ptrdiff\_t

_ptrdiff\_t_ is a special signed integer type defined in the standard library of C and C\+\+ languages\. It is a type of [pointer subtraction](https://timsong-cpp.github.io/cppwp/n4861/expr.add#5) result\. The behavior of the type is similar to [_size\_t_](https://pvs-studio.com/en/blog/terms/0044/): on a 32\-bit system, the size of _ptrdiff\_t_ will be 32 bits, on a 64\-bit system – 64 bits\.

Also, when working with standard library containers, the result of subtracting two iterators has the _difference\_type_ type of the container being used, which, depending on the standard library, is often equal to _ptrdiff\_t\._

The _ptrdiff\_t_ type is often used for [address arithmetic](https://pvs-studio.com/en/blog/terms/0005/) and array indexing if negative values are possible\. Programs that use regular integer types for this purpose may encounter [undefined behaviour](https://pvs-studio.com/en/blog/terms/0066/)\. For example, if the index value exceeds _INT\_MAX_\.

For arrays smaller than _PTRDIFF\_MAX_, _ptrdiff\_t_ behaves like an analog of _size\_t_: it can store the size of an array of any type and is synonymous with [_intptr\_t_](https://pvs-studio.com/en/blog/terms/0023/) on most platforms\. However, if the array is large enough \(larger than _PTRDIFF\_MAX_, but smaller than _SIZE\_MAX_\) and the difference of its pointers cannot be represented as _ptrdiff\_t_, the result of subtracting such pointers [is undefined](https://timsong-cpp.github.io/cppwp/n4861/expr.add#5.3)\.

In C, the _ptrdiff\_t_ type is declared in the _<stddef\.h\>_ header file\. In C\+\+, its declaration is located in _<cstddef\>_ and is placed in the global namespace and in _std_\. Standard header files of the C language for backward compatibility can also be included in C\+\+ programs\.

In terminology of the [PVS\-Studio](https://pvs-studio.com/en/pvs-studio/) static analyzer, the _ptrdiff\_t_ type refers to [memsize](https://pvs-studio.com/en/blog/terms/0030/) types\. The analyzer has a large number of special 64\-bit [diagnostic rules](https://pvs-studio.com/en/docs/warnings/#64CPP) issued by the analyzer and related specifically to the recommendation of implementing memsize types\. If you are planning to start developing cross\-platform projects or porting existing 32\-bit projects to 64\-bit systems, you may use [PVS\-Studio](https://pvs-studio.com/en/pvs-studio/) analyzer which can simplify this task greatly and allow you to avoid the long stage of searching for hidden errors\.

To learn more about the errors you can avoid when using _ptrdiff\_t_ type and also how this type allows improving and optimizing your programs, see the articles given in the references\. 

## References

1. [About size\_t and ptrdiff\_t](https://pvs-studio.com/en/blog/posts/cpp/a0050/)\.
1. [Optimization of 64\-bit programs](https://pvs-studio.com/en/blog/posts/cpp/a0030/)\.
1. [Seven steps of migrating a program to a 64\-bit system\.](https://pvs-studio.com/en/blog/posts/cpp/a0042/)
1. [A 64\-bit horse that can count](https://pvs-studio.com/en/blog/posts/cpp/a0043/)\.
1. [Lessons on development of 64\-bit C and C\+\+ applications](https://pvs-studio.com/en/blog/lessons/full/)\.