problem with g++ on cygwin
Thu Oct 20 13:30:00 GMT 2005
-----BEGIN PGP SIGNED MESSAGE-----
According to community help on 10/20/2005 7:06 AM:
> I'm using g++ on cygwin for developping some c++
> I noticed that everytime i manipulate pointers i have
> an error saying:
> "Segmentation Fault <Core Dumped>".
Well, it's because you are trying to modify read-only memory. Fix the bug
in your code. Your problem is not cygwin-specific, although cygwin is a
little less forgiving of memory usage errors, and more likely to core dump
when your code is buggy.
> #include <iostream.h>
> #include <string.h>
> int main()
> char * str;
> str = "hello";
The type of "hello" is const char*, because it lives in read-only memory.
You are (silently) casting away the const, and that is your bug; compile
with -Wall and you should be getting a warning.
> *(str+1) = 'a';
Oops - now you are trying to change a read-only location.
Now, had you declared this instead:
char str = "hello";
Then *(str+1) = 'a' is legal, because C++ allows a char initializer to
copy the contents of a string literal, without putting it in read-only memory.
Life is short - so eat dessert first!
Eric Blake firstname.lastname@example.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
-----END PGP SIGNATURE-----
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Problem reports: http://cygwin.com/problems.html
More information about the Cygwin