Consider the following code:
int* p1 = new int[100];
int* p2 = new int[100];
const ptrdiff_t ptrDiff = p1 - p2;
int* p1_42 = &(p1[42]);
int* p2_42 = p1_42 + ptrDiff;
Now, does the Standard guarantee that p2_42 points to p2[42]? If not, is it always true on Windows, Linux or webassembly heap?
We can perform arithmetic operations on the pointers like addition, subtraction, etc. However, as we know that pointer contains the address, the result of an arithmetic operation performed on the pointer will also be a pointer if the other operand is of type integer.
Pointer multiplication, division, and addition are not permitted because they do not make sense in pointer arithmetic. In Python, there are seven arithmetic operators: addition, subtraction, multiplication, division, and modulus, Exponentiation.
When a pointer is incremented, it actually increments by the number equal to the size of the data type for which it is a pointer. For Example: If an integer pointer that stores address 1000 is incremented, then it will increment by 2(size of an int) and the new address it will points to 1002.
Two pointers can also be subtracted from each other if the following conditions are satisfied: Both pointers will point to elements of same array; or one past the last element of same array. The result of the subtraction must be representable in ptrdiff_t data type, which is defined in stddef.
If we have two pointers p1 and p2 of base type pointer to int with addresses 1000 and 1016 respectively, then p2 - p1 will give 4, since the size of int type is 4 bytes. If you subtract p2 from p1 i.e p1 - p2 then the answer will be negative i.e -4. The following program demonstrates pointer arithmetic between two pointers of the same type.
Now let’s see how the two-pointer technique works. We take two pointers, one representing the first element and other representing the last element of the array, and then we add the values kept at both the pointers.
Pointer arithmetic is slightly different from arithmetic we normally use in our daily life. The only valid arithmetic operations applicable on pointers are: The pointer arithmetic is performed relative to the base type of the pointer.
We can't perform every type of arithmetic operations with pointers. Pointer arithmetic is slightly different from arithmetic we normally use in our daily life. The only valid arithmetic operations applicable on pointers are:
To add the standard quote:
expr.add#5
When two pointer expressions
PandQare subtracted, the type of the result is an implementation-defined signed integral type; this type shall be the same type that is defined asstd::ptrdiff_tin the<cstddef>header ([support.types]).
(5.1) If
PandQboth evaluate to null pointer values, the result is 0.(5.2) Otherwise, if
PandQpoint to, respectively, elementsx[i]andx[j]of the same array objectx, the expressionP - Qhas the valuei−j.(5.3) Otherwise, the behavior is undefined. [ Note: If the value
i−jis not in the range of representable values of typestd::ptrdiff_t, the behavior is undefined. — end note ]
(5.1) does not apply as the pointers are not nullptrs. (5.2) does not apply because the pointers are not into the same array. So, we are left with (5.3) - UB.
const ptrdiff_t ptrDiff = p1 - p2;
This is undefined behavior. Subtraction between two pointers is well defined only if they point to elements in the same array. ([expr.add] ¶5.3).
When two pointer expressions
PandQare subtracted, the type of the result is an implementation-defined signed integral type; this type shall be the same type that is defined asstd::ptrdiff_tin the<cstddef>header ([support.types]).
- If
PandQboth evaluate to null pointer values, the result is 0.- Otherwise, if P and Q point to, respectively, elements
x[i]andx[j]of the same array objectx, the expressionP - Qhas the valuei−j.- Otherwise, the behavior is undefined
And even if there was some hypothetical way to obtain this value in a legal way, even that summation is illegal, as even a pointer+integer summation is restricted to stay inside the boundaries of the array ([expr.add] ¶4.2)
When an expression
Jthat has integral type is added to or subtracted from an expressionPof pointer type, the result has the type ofP.
- If
Pevaluates to a null pointer value andJevaluates to 0, the result is a null pointer value.- Otherwise, if
Ppoints to elementx[i]of an array objectxwith n elements,81 the expressionsP + JandJ + P(whereJhas the valuej) point to the (possibly-hypothetical) elementx[i+j]if0≤i+j≤nand the expressionP - Jpoints to the (possibly-hypothetical) elementx[i−j]if0≤i−j≤n.- Otherwise, the behavior is undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With