getline => Segmentation fault

Apr 30, 2009 at 8:51pm
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 Apr 30, 2009 at 9:49pm
May 1, 2009 at 1:44am
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.
May 1, 2009 at 2:15am
I think that line may be a candidate for the Most Mistakes in a Single Line Award.
Last edited on May 1, 2009 at 2:31am
Topic archived. No new replies allowed.