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
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);
// ...
}
>> 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.
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!
Anyway, I changed the declaration to:
char Sl[1];
and everything works fine.
Yes, I'm using g++.
Why don't you read the replies in the thread.
> Yes, I'm using g++.
That does not matter.
Cheers & hdh.,
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?
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
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!