0

I am trying something different with cin.get() like given below:

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:

character array with size 30

Now we will try to enter more than 30 character in chArray using cin.get().

The character array size is 30 but we are entering more than 30 using cin.get() but the size is still 30.

How is size of chArray not changing from 30 to the size of the string we entered using cin.get()?

Please explain.

2
  • 2
    Because entering more characters into an array than the array can handle does not change the size of the array. It just makes your program invalid.
    – john
    Commented Jun 13, 2020 at 20:01
  • 2
    Arrays cannot change size. You declare an array of size X and it stays size X until it dies. std::string, on the other hand will change size when used with the correct functions. Commented Jun 13, 2020 at 20:13

2 Answers 2

1

A fixed array is not dynamically sizable. Once the array is declared, it cannot change size (sizeof() is fixed at compile-time). Your code has a buffer overflow that will corrupt surrounding memory if you try to enter more characters than the array can hold. In your example, your array can only hold 30 chars max, but you are telling cin that it can read up to 100 chars (well, 99, plus a null terminator) into the array.

For what you are trying to do, you need to read into a std::string instead of a char[] array. The size() of a std::string can change dynamically at runtime, eg:

#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;
2
  • Your code has a buffer overflow that will corrupt surrounding memory if you try to enter more characters than the array can hold. can you explain a bit more where the other characters are storing when array can only hold 30 character and how this surrounding memory corrution occur. it will be very helpfull. Commented Jun 13, 2020 at 20:38
  • 1
    @RahulBalyan the remaining chars are simply written to the memory that follows the array, overwriting whatever is in that memory. And you don't know what the app stores in that memory, if anything. Commented Jun 13, 2020 at 21:57
1

How is size of chArray not changing from 30 to the size of the string we entered using cin.get()?

Arrays in C++ have fixed size. 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. It basically means you crossed the boundary i.e., the fixed size set, which is 30 in this case.

The other characters entered (after the limit of 30) can go anywhere in the memory because it is undefined where they will 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. 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';
5
  • I wanted to know this thing is happening, if you can please explain me how this overflow of char array works Commented Jun 13, 2020 at 20:44
  • I got your point regarding buffer flow. but I am not getting an error at all, I am able to print the exact array which I entered using get function. Commented Jun 13, 2020 at 21:16
  • 2
    Its undefined behaviour, that means you may get an error if you are lucky, but you may not get an error at all or even bad output, but someday without any reason it might start giving errors. Undefined behaviour ;)
    – Waqar
    Commented Jun 13, 2020 at 21:26
  • 1
    "They are created on the stack with a fixed size given by the programmer" - in this example, yes. But fixed arrays can also be created on the heap, too. It would be more accurate to say that fixed arrays are always allocated in automatic memory, whether that be on the stack, or on the heap, or inside an object on the stack/heap, etc. Commented Jun 13, 2020 at 21:55
  • 1
    @RahulBalyan: Yes, it's undefined behaviour. If you run a short program, you may not use immediately the next memory address with some variable. Say, char word[6] {"hello"} starts at 100 and 6 bytes from there to 106 (including null). Now if you have defined another variable, say string at memory 107, you may get wierd or undefined behaviour, if buffer overrun happens. You can try to break your program by allocating memory at your choice, I believe there's something in C you could use to force allocation on a certain memory location. Try that you'll get wierd answers.
    – ppadhy
    Commented Aug 24, 2021 at 9:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.