What do these strange macro definitions mean (and are they even correct?)
I am working on some legacy C code and have come accross two strange macro definitions. They don't look right, and are also responsible for some compiler warnings ( warning: left-hand operand of comma expression has no effect ), which took me several hours to finally track down to these macros.
Can anyone tell me if they are correct (I suspect not), and if not, how do I fix them?
#define MAX_MEMORY_BLOCK (sizeof(size_t)==2,65535,2147483647)
#define MAX_ARRAY_SIZE (sizeof(size_t)==2,16384,1073741824)
They contain comma operators; only the last value 'counts', so they are equivalent to:
#define MAX_MEMORY_BLOCK (2147483647)
#define MAX_ARRAY_SIZE (1073741824)
Alternatively, someone forgot that the ternary operator uses ?:
:
#define MAX_MEMORY_BLOCK (sizeof(size_t)==2 ? 65535 : 2147483647)
#define MAX_ARRAY_SIZE (sizeof(size_t)==2 ? 16384 : 1073741824)
However, there are few modern systems where sizeof(size_t) == 2
(though there probably are some, especially in the embedded computing world).
What this really does, is:
1, 65535, 2147483647
1, 16384, 1073741824
or
0, 65535, 2147483647
0, 16384, 1073741824
in modern compilers.
And it's complaining because the first two expressions don't do anything. Normally when you separate operations by commas, it's because you want to cause something to happen at the same time. (In fact, I don't see them used very much at all.)
If you want to get rid of the warnings you could probably change them to be just:
#define MAX_MEMORY_BLOCK 2147483647
#define MAX_ARRAY_SIZE 1073741824
I'm not sure if those values actually make sense, though.
链接地址: http://www.djcxy.com/p/73618.html上一篇: C:宏产生警告“声明无效”