lousybyte lousybyte - 9 months ago 70
C++ Question

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";

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.

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


abc e


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

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

Answer Source

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.