VS2015: [C6386] Buffer Overrun while writing (even for same index value)
Visual C++ Code Analysis toolset may not always offer the best warnings. It tries to give you the best set of warnings to fix some potential issues/errors that may creep in at runtime. You have a few options:
- Disable the given warning around the code using
#pragma
directive. - Use C++ constructs:
new
,make_unique
etc. - (Not recommended) is to ignore the warning altogether and move on.
You should ideally always user newer smart pointers primitives like unique_ptr
, shared_ptr
etc. They not only allocate memory for you but deallocate on any exception thrown across the call stack. You don't need to type *
at all!
auto buffer = make_unique<int[]>(10); // 10 integers
Your code is fine and tools(especially analyzers) have their drawbacks — sometimes they generate false-positives. That's one of it. BTW, I checked your code on MSVS2015 and it gives me no warnings.