Pregunta

I'm currently updating some code and I ran into a spot where the code makes an assumption about the size of a time_t variable being the same as a signed long. This code works on our previous platform (so apparently on the Coldfire that was true) but we're moving to a new platform.

Now I know that we should not be making assumptions about the size of a time_t variable, since that's not well defined. But I really don't want to rewrite all the lines of code which work based on this assumption at the moment.

Ideally I'd like to do something like:

#if sizeof(time_t) != sizeof(LONG)
#error size assertion failed
#endif

once and be done with it. I'm aware that sizeof() can't be used in a conditional preprocessor like that so it's out of the question.

So I'm wondering, is there any way I can know the number of bytes that a time_t will take such that it can be used in a preprocessing conditional?

¿Fue útil?

Solución

If you're after a compile-time failure then just do something like this

{
    char s1[+sizeof(time_t) - sizeof(LONG) + 1];
    char s2[-sizeof(time_t) + sizeof(LONG) + 1];
}

Which will fail to build if the sizes differ.

You could put that into a macro, but the error would be rather cryptic.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top