简体   繁体   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;
    
}

What will happen if i tried to add 5th element in array of size 4?如果我尝试在大小为 4 的数组中添加第 5 个元素会发生什么?

Not really an answer, but this shows how to use std::array instead of a raw array:不是真正的答案,但这显示了如何使用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;
}

Depending on your platform and tools, debug builds show an error message when you access such an array out of bounds.根据您的平台和工具,当您越界访问此类数组时,调试版本会显示错误消息。

暂无
暂无

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

相关问题 在c ++中多次初始化数组时会发生什么? - what will happen when an array is initialized more than once in c++? 当我声明一个引用变量时,堆栈会发生什么? C ++ - What happen to stack when i declare a reference variable? C++ C ++ 11错误-当我尝试动态增加数组大小时,此错误是什么意思? - C++11 Error - What does this error mean when I try to dynamically increase the size of an array? 如果未分配返回未定义对象类型引用的C ++函数的返回值,会发生什么? - What should happen when the return value from a C++ function that returns a reference of an undefined object type is not assigned? c / c ++当size为0时strncmp的返回值是多少 - c/c++ what is the return value of strncmp when size is 0 如果我在 C/C++ 中定义一个 0 大小的数组会发生什么? - What happens if I define a 0-size array in C/C++? 当我在多个数组中有更多输入数据时,如何在 C++ 中创建排序结构? - How can I make a sorted structure in C++ when I have more input data in more than one array? 在 C++ 中,当没有为类声明构造函数时,如果我构造一个带参数的对象会发生什么? - In C++, when no constructor is declared for a class, what will happen if I construct an object with arguments? c ++ math lib域错误时会发生什么? - c++ math lib what happen when domain error? 当c ++ xx被批准时,命名空间tr1会发生什么? - What will happen to namespace tr1 when c++ xx is approved?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM