![](/img/trans.png)
[英]Error: C6386: Buffer overrun while writing to 'newArr': the writable size is 'int current_size*1' bytes, but '2' bytes might be written
[英]Buffer overrun while writing to 'array': the writable size is '1*4' bytes, but '8' bytes might be written
我正在用 0 到 100000 之間的隨機生成的數字填充一個空數組,但行“array[i] = rand() % 100000;” 有一條綠色下划線,並表示“寫入‘數組’時緩沖區溢出:可寫大小為‘1*4’字節,但可能寫入‘8’字節”。 有誰知道如何解決這一問題?
#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);
創建一個具有初始值N
的單個int
。
對於一個數組,寫new int[N];
要釋放這個 memory 你需要寫delete[] array;
: delete array;
將是未定義的。
請注意,為您處理所有 memory 管理的直接替換將是
std::vector<int> array(N);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.