For a pointer p, can p <p + 1 be false as a last resort?
Is it possible for a pointer variable p that p <( p + 1 ) is false? Please explain your answer. If so, under what circumstances could this happen?
I was wondering if p + 1 could overflow and be equal to 0.
For example . On a 64-bit PC with GCC-4.8 for a C program:
int main(void) { void *p=(void *)0xFFFFFFFFFFFFFFFF; printf("p :%p\n", p); printf("p+1 :%p\n", p+1); printf("Result :%d\n", p<p+1); } It returns:
p : 0xffffffffffffffff p+1 : (nil) Result : 0 Therefore, I believe that this is possible for this case. For an invalid pointer location, this can happen. This is the only solution I can think of. Are there any others?
Note: No assumptions are made. Consider any compiler / platform / architecture / OS where there is a chance that this can happen or not.
Is it possible for a pointer variable
pthatp<(p+1)is false?
If p points to a valid object (i.e. created in accordance with the C ++ object model) of the correct type, then no. p+1 will point to a memory location after this object and will always compare more than p .
Otherwise, the behavior of both arithmetic and comparison is undefined, so the result can be true, false or the suffix yellow.
If so, under what circumstances can this happen?
It may or may not happen to
p = reinterpret_cast<char*>(numeric_limits<uintptr_t>::max); If pointer arithmetic works like unsigned integer arithmetic, this can cause a numeric overflow, so p+1 has a value of 0 and compares less than p . Or it can do something else.
What if I program DOS and I have a far pointer (one consists of a segment and an offset), and this is pointing to the last address in the segment, and I add it to it, and the pointer goes around ? It looks like when you compare them, you normalize the pointers, so the second pointer p+1 will be less than p .
This is a hit in the dark, although I do not have a DOS C compiler that can be tested.
This can happen with an invalid pointer.
But if the pointer points to a valid memory location, in many operating systems (for example, Linux), this almost never happens (at least if sizeof(*p) not too large), since in practice the first and last pages are address space never displays (but you can force the display with mmap and MAP_FIXED ).
For stand-alone implementations (i.e., inside the kernel or on some microcontroller), everything is different, and the specific implementation (it may be undefined behavior or unspecified behavior ).
Very simple: This cannot happen if undefined behavior does not exist. This can happen very easily in the presence of undefined behavior. Read a copy of Standard C or Standard C ++ for more information.
As a result, the corresponding compiler is allowed not to evaluate the <operator, and instead use 1 or true. The same is true for arithmetic with integer signs (but not for unsigned integers, where a perfectly valid code has x> x + 1).
Your code example does not even have C or C ++, so you seem to have used the compiler in a mode where it is not a standard C or C ++ compiler.
According to comparisons of pointers in C. Are they signed or unsigned? in stack overflow:
You cannot legally compare arbitrary pointers in C / C ++. The result of this comparison is not defined.