Buffer-overrun,不知道为什么会发生这个警告?

huangapple go评论75阅读模式
英文:

Buffer-overrun, Don't know why this warning is occuring?

问题

以下是翻译好的部分:

编写了一个程序来创建一个动态数组,并将用户输入的新值附加到新数组 'arr2' 中,同时将前一个数组 'arr1' 中的元素复制到新的动态数组 'arr2' 中。程序运行并执行,但出现以下警告:“警告 C6386:写入 'arr2' 时的缓冲区溢出:可写大小为 'size*4' 字节,但可能写入 '8' 字节”。这是代码:

#include <iostream>
using namespace std;

void add(int array[], int size, int value);

void main()
{
	int s = 5, v = {}, arr1[5] = {1,2,3,4,5};
	
	cout << "\nHere is an array: ";
	for (int i = 0; i < 5; i++)
	{
		cout << arr1[i] << " ";
	}
	
	cout << "\nValue you want to add: ";
	cin >> v;
	add(arr1, s, v);

}
void add(int array[], int size, int value)
{
	size = size + 1;
	int* arr2 = new int[size];

	for (int i = 0; i < 5; i++)
	{
		arr2[i] = array[i];//警告在第 28 行发生

	}

	arr2[size - 1] = value;

	cout << "Here is the array with the added value: ";

	for (int i = 0; i < size; i++)
	{
		cout << arr2[i] << " ";
	}

	delete[] arr2;
}

以下是构建日志:

开始构建...
1>------ 开始构建: 项目: Practiceformid, 配置: Debug x64 ------
1>task2.cpp
1>C:\Users\ymaso\source\repos\Practiceformid\Practiceformid\task2.cpp(7,1): 警告 C4326: 'main' 的返回类型应为 'int',而不是 'void'
1>Practiceformid.vcxproj -> C:\Users\ymaso\source\repos\Practiceformid\x64\Debug\Practiceformid.exe
1>已完成构建项目“Practiceformid.vcxproj”。
========== 构建: 1 个成功,0 个失败,0 个最新,0 个跳过 ==========

我有点明白为什么会出现这个问题,可能是因为复制到新数组的前一个数组较小,导致新数组中留下一个空的索引,该索引可能返回 null 或垃圾值。但我稍后会用新值填充它。

英文:

Wrote a program to create a dynamic array and append a new value taken from the user into the new array 'arr2', While also copying the elements from the previous array 'arr1' into the new dynamic array 'arr2'. The program runs and executes but the following warning shows up "Warning C6386 Buffer overrun while writing to 'arr2': the writable size is 'size*4' bytes, but '8' bytes might be written". This is the code:

#include &lt;iostream&gt;
using namespace std;

void add(int array[], int size, int value);

void main()
{
	int s = 5, v={}, arr1[5] = {1,2,3,4,5};
	
	cout &lt;&lt; &quot;\nHere is an array: &quot;;
	for (int i = 0;i &lt; 5;i++)
	{
		cout &lt;&lt; arr1[i] &lt;&lt; &quot; &quot;;
	}
	
	cout &lt;&lt; &quot;\nValue you want to add: &quot;;
	cin &gt;&gt; v;
	add(arr1, s,v);

}
void add(int array[], int size, int value)
{
	size = size + 1;
	int* arr2 = new int[size];

	for (int i = 0;i &lt; 5;i++)
	{
		arr2[i] = array[i];//Warning Occurs here at line 28

	}

	arr2[size - 1] = value;

	cout &lt;&lt; &quot;Here is the array with the added value: &quot;;

	for (int i = 0;i &lt; size;i++)
	{
		cout &lt;&lt; arr2[i] &lt;&lt; &quot; &quot;;
	}

	delete[] arr2;
}

Here is the build-log:

Build started...
1&gt;------ Build started: Project: Practiceformid, Configuration: Debug x64 ------
1&gt;task2.cpp
1&gt;C:\Users\ymaso\source\repos\Practiceformid\Practiceformid\task2.cpp(7,1): warning C4326: return type of &#39;main&#39; should be &#39;int&#39; instead of &#39;void&#39;
1&gt;Practiceformid.vcxproj -&gt; C:\Users\ymaso\source\repos\Practiceformid\x64\Debug\Practiceformid.exe
1&gt;Done building project &quot;Practiceformid.vcxproj&quot;.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

I kind off have a hint why this is occuring maybe because the previous array that is being copied into the new array is small and it leaves an empty index in the new array that will return a null or garbage value. But I do later on fill it with the new value.

答案1

得分: 0

你的函数可以接受任何值作为"size"参数。如果"size"的值为1,那么新数组的大小将是2。

然后,你运行了一个循环5次,并访问了越界的值。

此外,你的大小可能会溢出(循环)加1。

对于你的程序,在固定大小为5的数组上调用函数时,不会发生任何事情。对于其他值,可能会出现问题。

循环应该运行直到"size-1"。

for (int i = 0; i < size-1; i++)

另外一点:

通常情况下,你应该避免使用有符号整数数据类型来表示数组的大小,请改用"size_t"。

英文:

Your function may be called with any value for "size". If size would be for example 1, then the new size of the new array would be 2.

Then you run your loop 5 times and access out of bound values.

Additionally your size my overflow (wrap around) by adding 1.

For your program, where you call your function with a fixed size array of 5, nothing will happen. For other values, there may be a problem.

The loop should run until "size-1".

for (int i = 0; i &lt; size-1; i++).

As a side note:

In general, you should avoid signed integer data types for sizes of arrays. please use size_t instead.

huangapple
  • 本文由 发表于 2023年6月5日 18:07:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定