簡體   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