英文:
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 <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];//Warning Occurs here at line 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;
}
Here is the build-log:
Build started...
1>------ Build started: Project: Practiceformid, Configuration: Debug x64 ------
1>task2.cpp
1>C:\Users\ymaso\source\repos\Practiceformid\Practiceformid\task2.cpp(7,1): warning C4326: return type of 'main' should be 'int' instead of 'void'
1>Practiceformid.vcxproj -> C:\Users\ymaso\source\repos\Practiceformid\x64\Debug\Practiceformid.exe
1>Done building project "Practiceformid.vcxproj".
========== 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 < 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论