0

I'm doing an intensive course in C++ and I am the kind of person that likes and wants to understand everything under the sun otherwise I can't continue with my homework.

char anything[5]{};
cin >> anything;
cout << anything << endl;

I can write 'Hello' in the console and it's all good. Writing 'H' is good too, and 'Hel' is good too. But what shouldn't be right is writing 'Helloo' or 'Hellooo', and it works! The moment I write 'Helloooooooooooooooooooooooooooooooo' program crashes, which is ok. This does the same when using numbers.

Why is this happening? I'm using CodeLite.

To my understanding, if I write over 5 characters the program shouldn't allow it, but it does.

What's the explanation behind this?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • *the program shouldn't allow it* Why do you think that? Your program does nothing to limit the amount of data input into that variable. Doing that is on you. – Shawn Dec 06 '20 at 15:50
  • That "intensive course in C++" might not be that good after all if it didn't teach you about undefined behaviour. Maybe you want a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead? – Lukas-T Dec 06 '20 at 15:59
  • Also note: `Hello` is 5 `char`s and you need room for a null terminator. – Ted Lyngmo Dec 06 '20 at 16:00
  • if the program does nothing to limit the amount of data input into that variable, why does it stop after I write Hellooooooo at a certain length? – Luigi Istratescu Dec 06 '20 at 16:00
  • @churill "Doing" i.e. not finished yet – Asteroids With Wings Dec 06 '20 at 16:03
  • OT: If you changed `char anything[5]{};` to `std::string anything;` and added the appropriate `#include ` then c++ would dynamically resize the string to fit the data entered and you would not have undefined behavior. – drescherjm Dec 06 '20 at 16:37
  • Disclaimer: but only if you use it as a drop-in replacement for the shown code; drescherjm is not saying that `anything[23487234]` will suddenly become valid – Asteroids With Wings Dec 06 '20 at 16:40

1 Answers1

4

[trigger warning: car crashes, amputations, cats]


if I write over 5 characters the program shouldn't allow it, but it does.

That's an overstatement.

The language doesn't allow it, but the compiler and runtime don't enforce that. That's because it would deduct from performance to perform bounds checking all the time just to look out for programmer errors, penalising those who didn't make any.

So, you broke a contract and what happens next is undefined. You could fall foul of some "optimisation" in the compiler that assumes you wrote a correct program; you could overwrite some memory at runtime, getting a crash; you could overwrite some memory at runtime, getting an apparently "valid" string up to the next NULL that happens to be there; or, you could open a wormhole to the local cattery. You just don't know.

This is called undefined behaviour.

An analogy would be driving over the speed limit. Your car won't stop you from speeding, but the law says not to, and if you do it anyway you may get arrested, or run someone over, or lose control then crash into a tree and lose all your limbs. Generally it's best not to take the chance.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
  • Thanks a lot for your answer, now I understand. Also, I've just realised now when I was reading you comment that what I just wrote up there is the structure of an array, right? -> char anything [5] {}; there would be 0 1 2 3 4 array elements in there, each with a value of 0. So if I want to display one, I could do anything[2], but why does it allow me to display it on screen as 'cout << anything; ? – Luigi Istratescu Dec 06 '20 at 16:05
  • @LuigiIstratescu `anything` is a five-element array of `char`, yes – Asteroids With Wings Dec 06 '20 at 16:07
  • Ok nvm, if I build and run, I can't do anything at all the program crashes, if I run only, I can do what I wrote above. – Luigi Istratescu Dec 06 '20 at 16:09
  • @LuigiIstratescu I'm not following you, sorry, and you keep changing your comment so it's hard for me to keep up. Good luck! – Asteroids With Wings Dec 06 '20 at 16:10