0

I've found the following code in a legacy project that seems to cause some trouble:

for( ; *str; ++str )
    *str = tolower(*str);

The trouble is, we get an SIGSEGV at the moment the result of tolower() should be written back to *str . This is what I got from using printf() debugging and the stracktrace produces, as we're using JNI here and thus attaching a debugger to the C libraries is not working for us.

The code fails if it's compiled with gcc on linux. Running the same thing on windows (CMake is being used for this cross-plattform setup) works fine.

Where can I look to find the reason for this? Any hints appreciated :)

thank you - Markus

Markus
  • 610
  • 1
  • 9
  • 23

1 Answers1

2

String literals are non-modifiable:

char blop[] = "modifiable string";
char *bla = "non-modifiable string";

*blop = tolower(*blop); // OK
*bla = tolower(*bla);   // not OK, modifying a string literal
ouah
  • 138,975
  • 15
  • 262
  • 325