简体   繁体   English

使用 cin.get() 初始化字符数组

[英]char array initialization using cin.get()

I am trying something different with cin.get() like given below:我正在尝试与cin.get()不同的东西,如下所示:

char chArray[30]="character array with size "; //current string size is 25 character with null character
cout<< chArray << sizeof(chArray)<< endl;
cout<< "Now we will try to enter more than 30 character in chArray using cin.get()";
cin.get(chArray,100);
cout<< chArray << endl << sizeof(chArray)<< endl;

output of above code is very strange as given below:上面代码的 output 很奇怪,如下所示:

character array with size 30大小为 30 的字符数组

Now we will try to enter more than 30 character in chArray using cin.get() .现在我们将尝试使用cin.get()chArray中输入超过 30 个字符。

The character array size is 30 but we are entering more than 30 using cin.get() but the size is still 30.字符数组大小为 30,但我们使用cin.get()输入的字符数超过 30,但大小仍为 30。

How is size of chArray not changing from 30 to the size of the string we entered using cin.get() ? chArray的大小如何不会从 30 变为我们使用cin.get()输入的字符串的大小?

Please explain.请解释。

A fixed array is not dynamically sizable.固定数组不是动态大小的。 Once the array is declared, it cannot change size ( sizeof() is fixed at compile-time).一旦声明了数组,它就不能改变大小( sizeof()在编译时是固定的)。 Your code has a buffer overflow that will corrupt surrounding memory if you try to enter more characters than the array can hold.如果您尝试输入的字符数超过数组可以容纳的字符数,您的代码会出现缓冲区溢出,这将破坏 memory 周围的内容。 In your example, your array can only hold 30 char s max, but you are telling cin that it can read up to 100 char s (well, 99, plus a null terminator) into the array.在您的示例中,您的数组最多只能容纳 30 个char ,但您告诉cin最多可以将100 个char (嗯,99 个,加上一个 null 终止符)读入数组。

For what you are trying to do, you need to read into a std::string instead of a char[] array.对于您要执行的操作,您需要读入std::string而不是char[]数组。 The size() of a std::string can change dynamically at runtime, eg: std::stringsize()可以在运行时动态变化,例如:

#include <string>

std::string str = "character string with size ";
std::cout << str << str.size() << std::endl;
std::cout << "Now we will try to enter more than 30 character in str using cin";
std::cin >> str; // or: std::getline(std::cin, str);
std::cout << str << std::endl << str.size() << std::endl;

How is size of chArray not changing from 30 to the size of the string we entered using cin.get()? chArray 的大小如何不会从 30 变为我们使用 cin.get() 输入的字符串的大小?

Arrays in C++ have fixed size. C++ 中的 Arrays 具有固定大小。 They are created on the stack with a fixed size given by the programmer.它们以程序员给定的固定大小在堆栈上创建。 That means you give them a specific size and it is known to the compiler at compile time.这意味着你给它们一个特定的大小,编译器在编译时就知道了。 This size does not change.这个大小不会改变。 Ever.曾经。

If you write more characters into the array than the size for example writing 100 characters in an array of size 30, it is called buffer overflow or buffer overrrun .如果您向数组中写入的字符多于大小,例如在大小为 30 的数组中写入 100 个字符,则称为缓冲区溢出缓冲区溢出。 It basically means you crossed the boundary ie, the fixed size set, which is 30 in this case.这基本上意味着您越过了边界,即固定大小集,在本例中为 30。

The other characters entered (after the limit of 30) can go anywhere in the memory because it is undefined where they will go.输入的其他字符(在 30 个限制之后)可以在 memory 中的任何位置进行 go,因为它们将在 go 的位置未定义。 If you try to print this array, your program will terminate with an error:如果您尝试打印此数组,您的程序将因错误而终止:

*** stack smashing detected ***: terminated

The error in this particular case means you tried to put more data into the stack than it's capacity.这种特殊情况下的错误意味着您尝试将比其容量更多的数据放入堆栈。

However, we have string in C++, which you can use if you want a container which changes its size as required.但是,我们在 C++ 中有string ,如果您想要一个根据需要更改其大小的容器,您可以使用它。 Example:例子:

std::string mystr;
std::cout << "Mystr size before: " << mystr.size() << '\n';
std::getline (std::cin, mystr);
std::cout << "Mystr size after: " << mystr.size() << '\n';

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

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