在VS中更改常量char * literal的元素

时间:2016-11-27 20:39:36

标签: c++ pointers char const

这是代码:

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



使用gcc 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)



使用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?^

没有/O2的编译也不会产生任何警告,但会在运行时崩溃

这是VS编译器错误还是我遗漏了什么?

1 个答案:

答案 0 :(得分:4)

我不认为这可以称为错误。使用a[1] = 'e';您正在更改只读内存。字符串文字不应该被修改。这实际上是一种未定义的行为。为什么VS编译器没有像gcc一样发出警告?好吧,每个程序(编译器都是程序)都可以更好。在这种特殊情况下,非常古老的编译器代码可以工作,因为这不是最近的C ++特性(如T&&引用)。旧代码可能无法给出最佳错误。

还有很多其他方法可以编写错误的代码。不幸的是,在C / C ++中,这相对容易。我们在这里。

有许多静态代码检查器和消毒剂(例如:CodeSonar)。通常他们不是免费的。他们遇到了很多问题,但不是全部问题。