为什么c ++接受将较大的字符串分配给较小的char数组?

时间:2017-07-03 13:55:03

标签: c++ arrays memory g++

例如,在执行以下代码时或之后我会发现某种错误消息:

#include <iostream>
using namespace std;

int main(){
    char name[10];

    cout << "Who are you?\n Enter your name: ";
    cin >> name;
    cout << "You are "<< name << "\n";

    return 0;
}

并编译和执行,例如 g ++ -Wall test.cpp -o test.x 并执行test.x

  

输入姓名:abcdefghijklmnopqrstuvyz

所以系统回复:

  你是:abcdefghijklmnopqrstuvyz

因此创建了一个类型为char的数组,其中包含10个位置,但它可以存储超过10个。因此,程序的内存空间比我预期的要多。 我想知道为什么编译器(在这种情况下是g ++)允许这个?

0 个答案:

没有答案