[英]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.