1

The C standard guarantees the validity of a pointer comparison when both point to elements of the same array, but how does that typically get ensured in a system?

The compiler might let you choose between signed and unsigned pointers. The compiler will be generating the assembly that does the comparison. But the compiler does not allocate the memory. For example, if you compile with signed pointers, how does the compiler know the runtime won't allocate a block for an array that spans the signed overflow break?

2
  • "signed pointers"? [Citation needed] May 13, 2013 at 21:21
  • Do you mean when converting an integer for use as a pointer? (And, ick!) May 13, 2013 at 21:22

3 Answers 3

1

The compiler might let you choose between signed and unsigned pointers. The compiler will be generating the assembly that does the comparison. But the compiler does not allocate the memory. For example, if you compile with signed pointers, how does the compiler know the runtime won't allocate a block for an array that spans the signed overflow break?

In other words, how does a typical implementation ensure that no user data spans address 0x80000000 or 0x00000000. Well, on the popular desktop operating systems, you get this guarantee for free, because 0x00000000 is in kernel space (inaccessible to your userspace programs) and 0x80000000 is... well, I don't actually know about 32-bit machines anymore. But on a 64-bit machine, 0x8000000000000000 is literally in the middle of nowhere — typical 64-bit OSes don't map anything at all in the gigantic range between 0x0000FFFFFFFFFFFF and 0xFFFF000000000000 (source). If you have tons of resources, then it's no problem at all to leave a few billion bytes unused. (Massive understatement alert!)

Now, if you're programming on bare metal, then you might not be guaranteed that &a[7] < &a[8]. This is particularly likely to be true if you're programming on a RISC target such as PowerPC or V800 and taking advantage of the "Small Data Area" located around address 0x0000 (which is quick to access because loading from those addresses doesn't require indirection through a register). In this situation, you're basically encouraging the compiler to split a variable across the 0x0000 boundary (and even worse, allow &v == NULL to be true), in exchange for a valuable optimization.

0

There are no mechanisms to enforce such correctness in C. It is up to you to write correct code. C is not a safe language. It is possible to write syntactically correct code that does not form a valid program.

0

AFAIK, memory addresses are always used unsigned. If your code uses them otherwise, it's not going to work well (unless you really know what you're doing).

1
  • 1
    Most compilers have command-line switches to toggle between "signed pointer comparisons" and "unsigned pointer comparisons". Portable code shouldn't care, but systems code (which non-portably compares unrelated pointers to raw memory) often does care. Oct 7, 2013 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.