So I've asked this question before, but deleted it, because it was unclear, and I couldn't come up with a minimal reproducible example. I'm using a variable idx
together with the ++
-operator to iterate over an array to add some stand alone data to it, meaning I don't want to put it in a for
-loop.
Now I did some "research" and I found that this
int main()
{
int idx = 0;
int integers[6];
integers[idx++] = 0;
integers[idx++] = 5;
integers[idx++] = 3;
integers[idx++] = 4;
integers[idx++] = 1;
integers[idx++] = 2;
}
does not cause a warning, while this
#include <string>
int main()
{
int idx = 0;
std::string* strings[6];
strings[idx++] = new std::string("Some");
strings[idx++] = new std::string("stand");
strings[idx++] = new std::string("-");
strings[idx++] = new std::string("alone");
strings[idx++] = new std::string("data");
strings[idx++] = new std::string(".");
}
causes the following warning when I run the program in debug mode (possibly because static code analysis is hard):
Warning C6386 Buffer overrun while writing to 'strings': the writable size is '48' bytes, but '56' bytes might be written.
The marked warning line isn't really important, since VS tends to mark a random line, but it's for me the fourth of the six memory allocations. So I was wondering why I'm getting this warning exactly, if and how I can get rid of it, without changing the idx++
to hardcoded indexes. I assume the problem is a combination of the ++
-operator and allocating objects.
Important note: this is on Visual Studio, with Debug > Project Properties > C/C++ > General > Warning Level > Level 4 enabled. Also, this is all code causing the warning on my VS.
Here is all I can find in Visual Studio's about (maybe something else is causing this?)
Microsoft Visual Studio Enterprise 2019 (Version 16.5.3) VisualStudio.16.Release/16.5.3+30002.166
Microsoft .NET Framework (Version 4.8.03752)
Installed Version: Enterprise
Visual C++ 2019 00433-90002-07609-AA105 Microsoft Visual C++ 2019
ASP.NET and Web Tools 2019 16.5.236.49856 ASP.NET and Web Tools 2019
Azure App Service Tools v3.0.0 16.5.236.49856 Azure App Service Tools v3.0.0
C# Tools 3.5.0-beta4-20153-05+20b9af913f1b8ce0a62f72bea9e75e4aa3cf6b0e C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.
Color Theme Designer 1.0 Designer for creating new color themes
Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.
Cookiecutter 16.5.20041.1 Provides tools for finding, instantiating and customizing templates in cookiecutter format.
IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info
Microsoft Azure Tools 2.9 Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.30207.1
Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines
Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers
Microsoft Visual C++ Wizards 1.0 Microsoft Visual C++ Wizards
Microsoft Visual Studio VC Package 1.0 Microsoft Visual Studio VC Package
NuGet Package Manager 5.5.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/
Productivity Power Tools 2017/2019 16.0 Installs the individual extensions of Productivity Power Tools 2017/2019
ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info
Python 16.5.20041.1 Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers.
Python - Conda support 16.5.20041.1 Conda support for Python projects.
Python - Django support 16.5.20041.1 Provides templates and integration for the Django web framework.
Python - IronPython support 16.5.20041.1 Provides templates and integration for IronPython-based projects.
Python - Profiling support 16.5.20041.1 Profiling support for Python projects.
Qt Visual Studio Tools 2.4.3 The Qt Visual Studio Tools allow developers to use the standard development environment without having to worry about any Qt-related build steps or tools.
Test Adapter for Boost.Test 1.0 Enables Visual Studio's testing tools with unit tests written for Boost.Test. The use terms and Third Party Notices are available in the extension installation directory.
Test Adapter for Google Test 1.0 Enables Visual Studio's testing tools with unit tests written for Google Test. The use terms and Third Party Notices are available in the extension installation directory.
TypeScript Tools 16.0.20225.2001 TypeScript Tools for Microsoft Visual Studio
Visual Basic Tools 3.5.0-beta4-20153-05+20b9af913f1b8ce0a62f72bea9e75e4aa3cf6b0e Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.
Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio
Visual Studio Tools for CMake 1.0 Visual Studio Tools for CMake