File: ptr_cast_test.c

package info (click to toggle)
libnet-ssleay-perl 1.36-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 940 kB
  • ctags: 798
  • sloc: perl: 3,668; ansic: 2,711; makefile: 7
file content (43 lines) | stat: -rw-r--r-- 1,232 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdlib.h>
#include <stdio.h>

/* test if a pointer can be cast to an unsigned long int and back
   (aspa@hip.fi)

   tested on: HP-UX B.10.20, AIX 4.3, IRIX 5.3, OSF1 v4.0B and SunOS 5.6
   with both gcc and native compilers, and linux/gcc (i686) +
   linux/gcc (alpha).

*/

#define FROMTYPE void *
#define FROMTYPESTR "void *"
#define TOTYPE unsigned long int
#define TOTYPESTR "unsigned long int"

int main(argc, argv)
     int argc; /* e.g. HP-UX cc doesn't support ISO C by default */
     char *argv[];
{
  /* heap should be near the end of process's address space */
  FROMTYPE bufptr = (FROMTYPE) malloc(500);
  volatile TOTYPE i; /* prevent optimization */

  printf("# %s: '%s' len: %ul, '%s' len: %ul.\n", argv[0], FROMTYPESTR,
	 (int)sizeof(TOTYPE), TOTYPESTR, (int)sizeof(char *));

  i = (TOTYPE)bufptr;
  if( ((FROMTYPE)i) != bufptr ) {
    printf("# %s: failed: (%p != %p).\n", argv[0], (FROMTYPE)i, bufptr);
    printf("# ERROR: a '%s' can't be cast to a '%s' and back \n",
	   FROMTYPESTR, TOTYPESTR);
    printf("# ERROR: without loss of information on this architecture.\n");
    exit(1);
  } else {
    printf("# ptr_cast_test: ok (%p == %p).\n", (FROMTYPE)i, bufptr);
    exit(0);
  }

  exit(1);
}