std::ptrdiff_t(3)
π std::ptrdiff_t(3) π
okay bestie, this is the std::ptrdiff_t(3) page from the C++ Standard Libary manual β yes, thatβs the manual name, typo and all, and weβre about to make pointer-difference math cute πβ¨
π NAME π
std::ptrdiff_t - std::ptrdiff_t
(yes, the NAME line is literally that repeat, no cap π )
π¦ Synopsis π¦
Defined in header <cstddef>
typedef /*implementation-defined*/ ptrdiff_t;
std::ptrdiff_t is the signed integer type of the result of subtracting two pointers. Itβs giving pointer mathβs official little difference holder π«Ά
The bit width of std::ptrdiff_t is not less than 17. (since C++11) β
π Notes π
std::ptrdiff_t is used for pointer arithmetic and array indexing, if negative values are possible. Programs that use other types, such as int, may fail on, e.g. 64-bit systems when the index exceeds INT_MAX or if it relies on 32-bit modular arithmetic. So yeah, this is not the moment to be quirky with int, bestie π
When working with the C++ container library, the proper type for the difference between iterators is the member typedef difference_type, which is often synonymous with std::ptrdiff_t.
Only pointers to elements of the same array (including the pointer one past the end of the array) may be subtracted from each other. Thatβs the rule, periodt π¨
If an array is so large (greater than PTRDIFF_MAX elements, but less than SIZE_MAX bytes), that the difference between two pointers may not be representable as std::ptrdiff_t, the result of subtracting two such pointers is undefined. Like, do not do this ππ
For char arrays shorter than PTRDIFF_MAX, std::ptrdiff_t acts as the signed counterpart of std::size_t: it can store the size of the array of any type and is, on most platforms, synonymous with std::intptr_t.
It is unspecified whether the declaration of std::ptrdiff_t is available in any other standard library header. An implementation may avoid introducing this name even when the standard requires std::ptrdiff_t to be used. So donβt assume itβs gonna pop up everywhere like an uninvited group-chat notification π
β¨ Possible implementation β¨
// valid since C++11
using ptrdiff_t = decltype(static_cast<int*>(nullptr) -
static_cast<int*>(nullptr));
π Example π
// Run this code
#include <cstddef>
#include <iostream>
int main()
{
const std::size_t N = 10;
int* a = new int[N];
int* end = a + N;
for (std::ptrdiff_t i = N; i > 0; --i)
std::cout << (*(end - i) = i) << ' ';
std::cout << '\n';
delete[] a;
}
Output:
10 9 8 7 6 5 4 3 2 1
π See also π
size_tβ unsigned integer type returned by thesizeofoperator (typedef)offsetofβ byte offset from the beginning of a standard-layout type to specified member (function macro)- C documentation for
ptrdiff_t
mwah π this page is from http://cppreference.com, dated 2024.06.10, and itβs the std::ptrdiff_t(3) manual page β love u, bye bestie! ππ¦π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms