Hacker News new | past | comments | ask | show | jobs | submit login
How to correctly cast a pointer to int in a 64-bit application? (viva64.com)
7 points by ProgC on Oct 20, 2011 | hide | past | favorite | 1 comment



It might not work with an int, but I'm virtually certain it works with a long int. I say that because I had to research whether I could use a long value as both a reference count when a struct was in use, and a free list pointer when a struct was not in use. I didn't want to declare the field as a clunky union, but as simply "long".

I researched the issue in the C Language Reference Manual, document 007-0701-150, available on the web as a PDF. Here are my notes:

"In the value structure, the reference count N is a long int, which guarantees that it can also be used as a pointer to link unused values on the free list, without my having to use a cumbersome union type.

The standard says:

  long ints are large enough to hold pointers in -n32 and -o32 mode.  Both are
  32 bits wide.

  long ints are large enough to hold pointers in -64 mode.  Both are 64 bits
  wide.
(C Language Reference Manual, document 007-0701-150, Appendix A, Section F.3.7 "Arrays and Pointers")

Assuming there are no other modes, I conclude that long ints are large enough to hold pointers, period.

As further evidence, the section titled "Integer and Floating Point Types" has a table of Storage Class Sizes which lists the size in bits for the various types in all three modes -o32, -n32, and -64. In all modes the size of a long equals the size of a pointer."




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: