-1

I am confused after the following C++ code executed:

#include <iostream>
using namespace std;

int main()
{
    char name[0];
    int roll;
    cout << "Enter your name: ";
    cin.get(name, 30);
    cout << "Enter roll no.: ";
    cin >> roll;
    cout << "You have entered:" << '\n';
    cout << "Name: " << name << '\n' << "Roll No. " << roll << '\n';
    return 0;
}

As you see, it simply takes input and print it. But the problem is, how cin is supposed to read all the characters i have entered if the value of array is 0!!!

The output is as follows:

Enter your name: Bharat Singh Chauhan
Enter roll no.: 12345
You have entered:
Name: Bharat Singh Chauhan
Roll No. 12345

Sorry if the question is asked before ;)

rioV8
  • 24,506
  • 3
  • 32
  • 49
Bharat
  • 29
  • 1
  • 1
  • 7
  • 1
    It's not. What happens, is that [demons fly out of your nose, instead](http://www.catb.org/jargon/html/N/nasal-demons.html). C++ will happily allow you to write broken code that randomly crashes and burns, on Mondays, Wednesdays, and Fridays, and runs fine, otherwise, on other days of the week. – Sam Varshavchik Aug 09 '20 at 14:35
  • 1
    Note, you can only accept at most one answer, so choose the one that best answers your question. You can upvote multiple answers if you want. – cigien Aug 09 '20 at 14:48
  • okay.. actually i am not fond of asking questions on this website.. inconvenience is regretted. – Bharat Aug 09 '20 at 14:50

3 Answers3

4

This code:

char name[0];
cin.get(name, 30);

invokes undefined behavior because name does not have enough space to store 30 chars. This means anything can happen, including the program appearing to work sometimes.

Instead, you can do:

std::string name;
std::cin >> name;
// or
std::getline(std::cin, name); // if you want to accept whitespace

which is less error prone in general than using arrays.

cigien
  • 57,834
  • 11
  • 73
  • 112
2

Because C/C++ doesn't check memory borders natively. It is undefined behavior, because you're overwriting memory that you didn't allocated here, so, probably, overwriting memory, that was allocated for another purpose, for another variables, structs, etc. With another conditions you can get segmentation failed error.

Why conditions for segmentation failed didn't met now? Because default stack size (memory where your variable is "allocated") is much larger by default and easily can handle your hand-types strings here.

Also compilers could have option stack protection. So, they will warn if you overrun stack memory. It will not really protect you from undefined behavior or overwriting your local variables, but can help to find the problem, not always, but when your overrun is large enough to detect it. See this link for more information: Stack smashing detected

Ihor Drachuk
  • 1,265
  • 7
  • 17
  • yeah.. i got something like that... *** stack smashing detected ***: terminated Aborted (core dumped).. i was wondering why this line came after the execution.. – Bharat Aug 09 '20 at 14:42
1

char name[0]; - that is an array of zero 0 characters. If you read more than 0 characters into it, you have undefined behaviour. Arrays don't automatically grow as you add stuff to them. For that you want std::vector or std::string.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70