Pointer comparisons on X86_64 different between 32 and 64 bit pointer sizes.
Posted: Sun Dec 04, 2022 11:06 pm
The program below using the X86 cross compiler (VSI C X7.4-547) gives different results than on Alpha or IA64. Relation comparisons between pointers treats the pointer values as signed if using 32 bit pointers and unsigned if using 64 bit pointers. On the other architecures, the pointers are unsigned in all cases.
Code: Select all
/*
* Test if pointer comparison treats pointers as signed, specifically if
* ((char *)(-1)) is the largest address.
*/
#include <stdio.h>
#include <stdlib.h>
#pragma required_pointer_size save
#pragma required_pointer_size 32
static void test_short ( void )
{
/* Test 32 bit pointers */
char buffer[500], *p, *p_max;
printf ( "\nShort pointer size: %d\n", sizeof(p) );
p = buffer; p_max = (char *)(-1);
printf ( " stack address %x is %s than %x\n",
p, (p<p_max) ? "less" : "NOT less", p_max );
}
#pragma required_pointer_size 64
static void test_long ( void )
{
/* Test 64 bit pointers */
char buffer[500], *p, *p_max;
printf ( "\nLong pointer size: %d\n", sizeof(p) );
p = buffer; p_max = (char *)(-1);
printf ( " stack address %llx is %s than %llx\n",
p, (p<p_max) ? "less" : "NOT less", p_max );
}
#pragma required_pointer_size restore
int main ( int argc, char **argv )
{
test_short();
test_long();
return 1;
}