繁体   English   中英

与 c++ 中指定的数组大小相比,当我尝试再添加一个值时会发生什么?

[英]What will happen when i try to add one more value compared to the specified size of array in c++?

#include <iostream>
using namespace std;

int main()
{
    int marks[4]={40,10,25,56};
    marks[5]=99;
    
}

如果我尝试在大小为 4 的数组中添加第 5 个元素会发生什么?

不是真正的答案,但这显示了如何使用std::array而不是原始数组:

#include <iostream>
#include <array>

using namespace std;

int main()
{
//  int marks[4] = { 40,10,25,56 };
  std::array<int, 4> marks = { 40,10,25,56 };

  marks[5] = 99;
}

根据您的平台和工具,当您越界访问此类数组时,调试版本会显示错误消息。

暂无
暂无

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

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