getline - Segmentation fault

M

mamboknave

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

char *const SL = "";
parse_usr_data(SL);
....
void parse_usr_data(char *SL)
{
....
std::cin.getline(SL,2);
....
}
 
S

Sjouke Burry

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

char *const SL = "";
parse_usr_data(SL);
...
void parse_usr_data(char *SL)
{
...
std::cin.getline(SL,2);
...
}
Because you put 3 chars in a string with length 1??
 
D

Default User

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

char *const SL = "";
parse_usr_data(SL);
...
void parse_usr_data(char *SL)
{
...
std::cin.getline(SL,2);
...
}

SL points to a string literal, which has a size of one. For purposes of
backward compatibility, a pointer to such a string literal can be
assigned to a pointer to char. That, however, should never be done, as
it is not modifiable storage. So you have two problems.

1. You pass in this pointer to parse_usr_data(), which then attempts to
modify the buffer pointed to. That is undefined behavior.

2. Even if SL was modifiable, it can only hold one character.


You should be std::string rather than char*, and the verison of getline
that is not a member of the instream class:

istream& std::getline( istream& is, string& s, char delimiter = '\n' );





Brian
 
A

Alf P. Steinbach

* Jeff Schwab:
To my surprise, g++ does not warn about this conversion unless
-Wwrite-strings is explicitly specified. -Wall doesn't cut it.

Thank you.

One more to add to that bunch of options...


Cheers,

- Alf
 
M

mamboknave

Thanks to everyone. I may be stubborn but I've hard time to get it.
The excuse is that it's just a few weeks I' fighting with C & C++.

Anyway, I changed the declaration to:

char Sl[1];

and everything works fine.

Yes, I'm using g++.
 
A

Alf P. Steinbach

* (e-mail address removed):
Thanks to everyone. I may be stubborn but I've hard time to get it.
The excuse is that it's just a few weeks I' fighting with C & C++.

Anyway, I changed the declaration to:

char Sl[1];

and everything works fine.

Why don't you read the replies in the thread.

Yes, I'm using g++.

That does not matter.


Cheers & hdh.,

- Alf
 
D

Default User

Thanks to everyone. I may be stubborn but I've hard time to get it.
The excuse is that it's just a few weeks I' fighting with C & C++.

Anyway, I changed the declaration to:

char Sl[1];

and everything works fine.

Once again, you have a character array that can hold exactly one
character. That did not at all seem to be what you wanted. I doubt very
much that it is "working fine". It's just not as obviously broken as it
was before.





Brian
 
J

Jorgen Grahn

To my surprise, g++ does not warn about this conversion unless
-Wwrite-strings is explicitly specified. -Wall doesn't cut it.

Earlier gcc versions (like 4.1) worked like that, but 4.3 is the other
way around. You don't have to specify *any* options to get that
particular warning.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top