I need know what is the size of a address of one variable, in other words, how much bytes I need to store the address of a variable (in compilation time).

Unhappily not is possible use sizeof in define pre-processors, like this:

#define ADDRESS_SIZE    sizeof(void *)

So, I try this:

#define ADDRESS_SIZE   ((char *)((void**)0 + 1) - (char*)(void**)0)

Apparently this work but, in this way I cannot do this test:

#if ADDRESS_SIZE == 4
typedef uint32_t address_pointer;
#endif

#if ADDRESS_SIZE == 8
typedef uint64_t address_pointer;
#endif

This return the error: "operator '*' don't have operator to right"

Can anyone help me with this problem?


Thanks,
Ricardo.

Recommended Answers

sizeof is a compile-time operation. The #if is part of the translation phase (which happens before compilation). So you can not use the compiled results before it is computed.

You can either use a type that is designed for this already ( uintptr_t ) or have your build …

Jump to Post

All 2 Replies

sizeof is a compile-time operation. The #if is part of the translation phase (which happens before compilation). So you can not use the compiled results before it is computed.

You can either use a type that is designed for this already ( uintptr_t ) or have your build system pass the value in as a compiler option ( -DADDRESS_SIZE=8 with gcc, for example).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.