Question Is there a preprocessor to calculate pointer size?

loveandkindess

New member
Joined
Dec 21, 2024
Messages
1
Programming Experience
1-3
This is my code in C:
#if UINTPTR_MAX == 0xFFFFu
typedef uint16_t GRAINS_UINTPTR;
#elif UINTPTR_MAX == 0xFFFFFFFFu
typedef uint32_t GRAINS_UINTPTR;
#elif UINTPTR_MAX == 0xFFFFFFFFFFFFFFFFu
typedef uint64_t GRAINS_UINTPTR;
#endif
How would one do this in C#?
 
No. The C# #if only checks for symbols being defined or not defiend. It cannot (currently) check for values, since C# symbol definitions only create entries in the symbol table -- they don't define values.

Just use the UIntPtr struct. It will be the correct size for your compilation target.

 
Last edited:
And then you can use a using alias to do:
C#:
using GrainsUIntPtr = System.UIntPtr;

See
 
Back
Top Bottom