简体   繁体   English

为什么我的代码在尝试复制字符数组时会抛出 C6386 错误?

[英]Why is my code throwing a C6386 error when trying to copy a character array?

i am getting a c++ warning C6386 on the following code:我在以下代码中收到 C++ 警告 C6386:

int m_wordNumber = getRandomNumber();
//get word size
size_t m_wordSize = strlen(m_wordsArray[m_wordNumber]);
//create word arrays
char* m_chosenWord = new char[m_wordSize];
char* m_blankArray = new char[m_wordSize];

int m_incorrectGuessCounter = 0;

generateArray(m_blankArray, m_wordSize);
//copy word to new array
strcpy_s(m_chosenWord, m_wordSize + 1, m_wordsArray[m_wordNumber]);
//create temp array and output word
char * m_tempWordHolder = nullptr;
std::cout << m_chosenWord << std::endl;

The line that is throwing the warning is the strcpy_s line, all it's trying to do is copy a string from a character array to a different character array.抛出警告的行是 strcpy_s 行,它试图做的就是将字符串从字符数组复制到不同的字符数组。

Any help would be greatly appreciated, thanks.任何帮助将不胜感激,谢谢。

the warning is as follows:警告如下:

Warning C6386 Buffer overrun while writing to 'm_chosenWord': the writable size is 'm_wordSize*1' bytes, but '7' bytes might be written警告 C6386 写入“m_chosenWord”时缓冲区溢出:可写大小为“m_wordSize*1”字节,但可能写入“7”字节

When you allocate m_chosenWord , you allocate m_wordSize characters.当您分配m_chosenWord ,您将分配m_wordSize字符。 However, in然而,在

strcpy_s(m_chosenWord, m_wordSize + 1, m_wordsArray[m_wordNumber]);

You are writing m_wordSize + 1 characters.您正在编写m_wordSize + 1字符。 In the initial allocation, you want to add the +1 as well.在初始分配中,您还想添加+1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么使用strsafe.h StringCch函数会导致C6386缓冲区溢出错误? - Why error C6386 buffer overrun with strsafe.h StringCch functions? 为什么在Visual Studio 2012的代码分析中,此代码为什么发出缓冲区溢出警告(C6385 / C6386)? - Why does this code emit buffer overrun warnings(C6385/C6386) in code analysis on Visual Studio 2012? 如何解决 C6386 警告? - How to solve C6386 warning? 避免“缓冲区溢出” C6386警告 - Avoiding 'Buffer Overrun' C6386 warning __m256 阵列上的 Visual Studio 2019 C6385 / C6386(缓冲区溢出警告) - Visual Studio 2019 C6385 / C6386 (buffer overrun warning) on __m256 array 无法解决警告 C6386 - Cannot solve warning C6386 Visual Studio 2015 代码分析 C6386 警告缓冲区溢出 - Visual Studio 2015 Code Analysis C6386 warns of buffer overrun 使用动态分配的 arrays 导致来自代码分析的 C6386 缓冲区溢出警告 - Using dynamically-allocated arrays causes C6386 Buffer Overrun warning from Code Analysis 为什么我在写入“ptr”时收到“C6386”缓冲区溢出警告? - Why am I getting warning 'C6386' Buffer overrun while writing to 'ptr'? 错误:C6386:写入“newArr”时缓冲区溢出:可写大小为“int current_size*1”字节,但可能写入“2”字节 - Error: C6386: Buffer overrun while writing to 'newArr': the writable size is 'int current_size*1' bytes, but '2' bytes might be written
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM