-1
#include<iostream>
using namespace std;
int main(void)
{
    char name[5];
    cout << "Name: ";
    cin.getline(name, 20);
    cout << name;
}

Output:

Name: HelloWorld
HelloWorld

Shouldn't this give an error or something?

Also when I write an even longer string,

Name: HelloWorld Goodbye
HelloWorld Goodbye

cmd exits with an error.

How is this possible?

Compiler: G++ (GCC 7), Nuwen OS: Windows 10

user2736738
  • 30,591
  • 5
  • 42
  • 56
Harshit Joshi
  • 260
  • 4
  • 15
  • 1
    Probably invoking UB – mdatsev Jan 20 '18 at 12:30
  • "*Shouldn't this give an error or something.*" No, generally speaking wrong code in C++ doesn't produce errors, it just makes your program behave in weird and unexpected ways. That's why you have to be very, very, very careful to write bug-free code in C++. – melpomene Jan 20 '18 at 12:36
  • A related post: [Can a local variable's memory be accessed outside its scope?](https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope/6445794#6445794) – default Jan 20 '18 at 12:47
  • I didn't know the term 'buffer overflow'. Thanks for all the answers. – Harshit Joshi Jan 20 '18 at 16:44
  • Marking as duplicate. – Harshit Joshi Jan 20 '18 at 16:45

5 Answers5

1

It's called buffer overflow and is a common source of code bugs and exploits. It's the developers responsibility to ensure it doesn't happen. character strings wil be printed until they reach the first '\0' character

newbie
  • 558
  • 7
  • 12
0

The code produces "undefined behavior". This means, anything might happen. In your case, the program works unexpectedly. It might however do something completely different with different compiler flags or on a different system.

Shouldn't this give an error or something.

No. The compiler cannot know that you will input a long string, thus there cannot be any compiler error. You also don't throw any runtime exception here. It is up to you to make sure the program can handle long strings.

wolff
  • 426
  • 4
  • 11
0

Your code has encountered UB, also known as undefined behaviour, which, as Wikipedia defines, the result of executing computer code whose behavior is not prescribed by the language specification to which the code adheres. It usually occurs when you do note define variables properly, in this case a too small char array.

QuIcKmAtHs
  • 297
  • 4
  • 18
0

Even -Wall flag will not give any warning. So you can use tools like valgrind and gdb to detect memory leaks and buffer overflows

Eziz Durdyyev
  • 1,110
  • 2
  • 16
  • 34
0

You can check those questions:

Array index out of bound in C

No out of bounds error

They have competent answers.

My short answer, based on those already given in the questions I posted:

  • Your code implements an Undefined Behavior(buffer overflow) so it doesn't give an error when you run it once. But some other time it may give. It's a chance thing.
  • When you enter a longer string, you actually corrupt the memory (stack) of the program (i.e you overwrite the memory which should contain some program-related data, with your data) and so the return code of your program ends up being different than 0 which interprets as an error. The longer the string, the higher the chance of screwing things up (sometimes even short strings screw things up)

You can read more here: https://en.wikipedia.org/wiki/Buffer_overflow

Sebi
  • 428
  • 5
  • 11