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;
}