In my code, I am using an array xyz
of 10 objects. When I am trying to access an element of the array using an unsigned int index like this: xyz[level]
, I get 'Buffer overrun' warning. Logically, I am pretty sure that level won't exceed 10. How to avoid this warning?
views:
103answers:
2
+9
A:
I'm probably teaching my grandmother to suck eggs here, but do remember that "level won't exceed 10" is wrong for an array of size 10:
char a[10];
a[10] = '\0'; // Bug, and "Buffer Overrun" warning.
RichieHindle
2010-06-11 10:52:04
+1, because I'm pretty sure that's exactly the reason why the OP gets the warning.
Nick D
2010-06-11 11:00:39
Yes, this is somewhat related to my issue. I did a check like this `if (level < 10)` before accessing like this `xyz[level]` and the warning vanished.
bdhar
2010-06-17 11:04:20
A:
Are you really sure? I never got this warning until now. So, double check.
Anyway, you can use the
#pragma warning( disable: 6386 )
preprocessor directive. I usually push and pop this to the "pragma stack"
#pragma warning( push )
#pragma warning( disable : 6386 )
// Some code
#pragma warning( pop )
as advised here.
mkluwe
2010-06-11 10:53:29
That is fairly dangerous as the warning might be caused by a bug. Only ever silence warnings if you are **CERTAIN** what caused them and that it is harmless.
Michael J
2010-06-12 17:23:48