Domanda

Motivo principale per questo è tentativo di scrivere una libreria C perfettamente portatile.Dopo alcune settimane ho finito con costanti, purtroppo non molto flessibili (usando costanti per la definizione di altre costanti non è possibile). THX per qualsiasi consiglio o critico.

È stato utile?

Soluzione

What you ask of is impossible. As stated before me, any standards compliant implementation of C will have limits.h correctly defined. If it's incorrect for whatever reason, blame the vendor of the compiler. Any "dynamic" discovery of the true limits wouldn't be possible at compile time, especially if you're cross compiling for an embedded system, and thus the target architecture might have smaller integers than the compiling system.

To dynamically discover the limits, you would have to do it at run-time by bit shifting, multiplying, or adding until an overflow is encountered, but then you have a variable in memory rather than a constant, which would be significantly slower. (This wouldn't be reliable anyways since different architectures use different bit-level representations, and arithmetic sometimes gets a bit funky around the limits especially with signed and abstract number representations such as floats)

Just use standard types and limits as found in stdint.h and limits.h, or try to avoid pushing the limits all together.

Altri suggerimenti

First thing that comes to my mind: have you considered using stdint.h? Thanks to that your library will be portable across C99-compliant compilers.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top