How to fix error c2118: negative index

Again, porting a 32-bit application to a 64-bit one. I get a negative index error in the C_ASSERT statement mentioned below.

C_ASSERT (sizeof(somestruct) == some#define); 

I also read the article http://support.microsoft.com/kb/68475 , but I'm not sure if I know how to fix it in this case.

Help is appreciated.

Thanks in advance.

+4
source share
1 answer

I assume that the C_ASSERT macro is defined as follows:

 #define C_ASSERT(x) typedef char C_ASSERT_ ## __COUNTER__ [(x) ? 1 : -1]; 

This is a compilation time statement: if the compilation time expression x true, then it expands to about

 typedef char C_ASSERT_1[1]; 

which declares a type name C_ASSERT_1 as an alias for type char[1] (an array of 1 char ). Converely, if the expression x false, it expands to

 typedef char C_ASSERT_1[-1]; 

which is a compiler error, since you cannot have a type of negative array size.

Therefore, your problem is that the expression sizeof(somestruct) == some#define is false, i.e. the size of somestruct NOT what your code expects. You need to fix this - either resize somestruct or change the value of some#define , making sure that it doesn't break anything.

+10
source

All Articles