gpt4 book ai didi

c++ - 为什么 cin.get(char, int) 不会溢出?

转载 作者:行者123 更新时间:2023-11-30 05:10:45 24 4
gpt4 key购买 nike

<分区>

这是我的代码:

#include <iostream>
#include <typeinfo>

using namespace std;


int main() {
const int size = 10;
char test[size];
char test2[size];

cout << "the size of test is " << sizeof(test) << endl;

cout << "input a sentence:" << endl;
cin.getline(test, 50);

cout << "your input is: " << test << endl;
cout << "the size of test is " << sizeof(test) << endl;

cout << "-----stop-----" << endl;
return 0;

}

我在 Clion 中用 MinGw-w64 3.1 测试了它,结果是这样的:

the size of test is 10
input a sentence:
this is a very long sentence
this is a very long sentence
your input is: this is a very long sentence
the size of test is 10
-----stop-----

Process finished with exit code -1073741819 (0xC0000005)

直到我完成所有输出,它才停止。它不应该在我尝试读取数组测试时停止吗?毕竟,我声称尺寸为 10,但尝试读取尺寸为 50。

然后我在 Ubuntu 下用 gcc 5.4.0 测试了这个,它没有给出任何错误信息!我想知道为什么它没有停止?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com