[英]Buffer overrun while writing to 'array': the writable size is '1*4' bytes, but '8' bytes might be written
I am filling an empty array with randomly generated numbers between 0 and 100000 but the line "array[i] = rand() % 100000;"我正在用 0 到 100000 之间的随机生成的数字填充一个空数组,但行“array[i] = rand() % 100000;” has a green underline and says "Buffer overrun while writing to 'array': the writable size is '1*4' bytes, but '8' bytes might be written".有一条绿色下划线,并表示“写入‘数组’时缓冲区溢出:可写大小为‘1*4’字节,但可能写入‘8’字节”。 Does anyone know how to fix this?有谁知道如何解决这一问题?
#include <iostream>
#include <Windows.h>
#include <time.h>
#pragma
using namespace std;
int main() {
int N;
cout << "Enter a value for N: ";
cin >> N;
int* array = new int(N);
srand(time(0));
for (int i = 0; i < N; i++) {
array[i] = rand() % 100000;
}
for (int i = 0; i < N; i++) {
cout << array[i] << ", ";
}
delete array;
return 0;
}
new int(N);
creates a single int
with the initial value N
.创建一个具有初始值N
的单个int
。
For an array, write new int[N];
对于一个数组,写new int[N];
To free this memory you need to write delete[] array;
要释放这个 memory 你需要写delete[] array;
: the behaviour of delete array;
: delete array;
will be undefined.将是未定义的。
Note that a drop-in replacement that takes care of all the memory management for you would be请注意,为您处理所有 memory 管理的直接替换将是
std::vector<int> array(N);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.