getline => Segmentation fault

From the keyboard I enter just one character and push Return key.
Then this getline goes in Segmentation fault.
Why so???

1
2
3
4
5
6
7
8
9
char *const SL = "";
parse_usr_data(SL);
...
void parse_usr_data(char *SL)
{
...
std::cin.getline(SL,2);
...
}
Last edited on
You are making a char* with no elements, and then trying to assign data to it. This means you will be going out of bounds of the array. I would suggest using an std::string instead if you can. Also, you defined it as const, which means you cannot modify it, and you will get a segfault when writing to the read-only memory.
I think that line may be a candidate for the Most Mistakes in a Single Line Award.
Last edited on
Topic archived. No new replies allowed.