Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

getline - Segmentation fault

0 views
Skip to first unread message

mambo...@gmail.com

unread,
Apr 30, 2009, 6:17:01 PM4/30/09
to
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);
...
}

Sjouke Burry

unread,
Apr 30, 2009, 6:33:15 PM4/30/09
to
Because you put 3 chars in a string with length 1??

Default User

unread,
Apr 30, 2009, 6:38:31 PM4/30/09
to
mambo...@gmail.com wrote:

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

Jeff Schwab

unread,
Apr 30, 2009, 6:45:40 PM4/30/09
to
mambo...@gmail.com wrote:
> From the keyboard I enter just one character and push Return key.
> Then this getline goes in Segmentation fault.
> Why so???
>
> char *const SL = "";

You're not allowed to write to overwrite that string. You probably
should not even be allowed to assign it to a char*, but this is allowed
as a special case, to avoid breaking some very old C code. Your
compiler should have warned you about this.

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

#include <iostream>
#include <string>

std::string getline(std::istream& in) {
std::string line;
getline(in, line);
return line;
}

int main() {
std::string const line = getline(std::cin);
// ...
}

Jeff Schwab

unread,
Apr 30, 2009, 6:50:13 PM4/30/09
to
Jeff Schwab wrote:
> mambo...@gmail.com wrote:

>> char *const SL = "";
>
> You're not allowed to write to overwrite that string.

> Your compiler should have warned you about this.

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

Alf P. Steinbach

unread,
Apr 30, 2009, 6:56:41 PM4/30/09
to
* Jeff Schwab:

Thank you.

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


Cheers,

- Alf

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!

mambo...@gmail.com

unread,
Apr 30, 2009, 8:07:18 PM4/30/09
to
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++.

Alf P. Steinbach

unread,
Apr 30, 2009, 8:36:59 PM4/30/09
to
* mambo...@gmail.com:

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


> Yes, I'm using g++.

That does not matter.


Cheers & hdh.,

Jeff Schwab

unread,
May 1, 2009, 7:49:37 AM5/1/09
to
mambo...@gmail.com wrote:
> 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++.

Those are very different languages. It's probably not a superb idea to
try learning them at the same time.

> Anyway, I changed the declaration to:
>
> char Sl[1];
>
> and everything works fine.

Why are you using a raw array of char to represent a string?

Default User

unread,
May 1, 2009, 1:52:16 PM5/1/09
to
mambo...@gmail.com wrote:

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

Jorgen Grahn

unread,
May 2, 2009, 1:31:40 PM5/2/09
to

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

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!

0 new messages