简体   繁体   English

c ++ int 数组上的迭代异常

[英]c++ iteration exception over int array

why does the code cause exception ?为什么代码会导致异常?

exception: Access violation writing location 0x000001A88BF5D000.异常:访问冲突写入位置 0x000001A88BF5D000。

int* ints = new int(1000000);
for (int i = 0; i < 1000000; i++)
{
    ints[i] = 1;
}

You're allocating a single integer.您正在分配一个整数。 If you want to allocate an integer array, you should use square brackets.如果你想分配一个整数数组,你应该使用方括号。

int* ints = new int[1000000]();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM