Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing elements of a constant char* literal in VS

This is the code:

int main()  
{  
    char* a = "abc";    // Should be 'const char*', but no warnings whatsoever on VS
    a[1] = 'e';         // No warnings on VS either 

    std::cout<< a << " " << a[1];  

    return 0;  
} 



Compiled using gcc version 6.2.0:

>g++ -O2 -o Test Test.cpp
Test.cpp: In function ‘int main()’:
Test.cpp:5:15: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
 char* a = "abc";
           ^~~~~

>./Test
Segmentation fault (core dumped)



Compiled using VS 2015.3:

>cl /EHsc /W4 /WX /O2 Main.cpp  
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86  
Copyright (C) Microsoft Corporation.  All rights reserved.  

Main.cpp  
Microsoft (R) Incremental Linker Version 14.00.24215.1  
Copyright (C) Microsoft Corporation.  All rights reserved.  

/out:Main.exe  
Main.obj   

>Main.exe  
abc e  

HUH?^

Compiling without /O2 yields no warnings either, but crashes on runtime.


Is this a VS compiler bug or I'm missing something?


1 Answers

I do not think this can be called a bug. With a[1] = 'e'; You are changing the read only memory. String literals are not expected to be modified. This is effectively an undefined behavior. Why VS compiler is not giving a warning, like gcc? Well, every program (compiler is a program either) can be better. In this particular case very old piece of compiler code works because this is not a recent C++ feature (like T&& references). Old code may not give out the best error.

There are tons of other ways to write bad code. Unfortunately in C/C++ this is relatively easy. Here we are.

There are numerous static code checkers and sanitizers (ex: CodeSonar). Typically they are not free. They catch a lot of problems but not all of them.

like image 114
Kirill Kobelev Avatar answered Dec 06 '25 23:12

Kirill Kobelev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!