banner
cells

cells

为美好的世界献上 code

Q188

Q188#

#include <iostream>

int main() {
    char *a = static_cast<char *>("Hello");
    a[4] = '\0';
    std::cout << a;
}

Answer#

click to see answer The program is undefined

Explain#

Modifying string literals is undefined behavior. In practice, the implementation can for instance store the string literal in read-only memory, such as the code segment. Two string literals might even be stored in overlapping memory. So allowing them to be modified is clearly not a good idea.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.