Skip to content

Instantly share code, notes, and snippets.

@chmullig
Last active December 30, 2015 05:19
Show Gist options
  • Save chmullig/7781437 to your computer and use it in GitHub Desktop.
Save chmullig/7781437 to your computer and use it in GitHub Desktop.
Testing String Literal comparisons
clm2186@athens:~$ clang++ -g -Wall test.cpp
test.cpp:5:58: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
std::cout << "\"hello\" == \"world\" = " << ("hello" == "world") << std::endl;
~~~~~~~ ^
test.cpp:6:58: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
std::cout << "\"hello\" == \"hello\" = " << ("hello" == "hello") << std::endl;
~~~~~~~ ^
test.cpp:7:57: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
std::cout << "\"hello\" < \"world\" = " << ("hello" < "world") << std::endl;
~~~~~~~ ^
test.cpp:8:55: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
std::cout << "\"hello\" < \"abc\" = " << ("hello" < "abc") << std::endl;
~~~~~~~ ^
4 warnings generated.
clm2186@athens:~$ ./a.out
"hello" == "world" = 0
"hello" == "hello" = 1
"hello" < "world" = 1
"hello" < "abc" = 1
hello is @ 0x400b22
hello is @ 0x400b22
world is @ 0x400b28
abc is @ 0x400b6c
clm2186@athens:~$ clang++ -g -Wall -fwritable-strings test.cpp
test.cpp:5:58: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
std::cout << "\"hello\" == \"world\" = " << ("hello" == "world") << std::endl;
~~~~~~~ ^
test.cpp:6:58: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
std::cout << "\"hello\" == \"hello\" = " << ("hello" == "hello") << std::endl;
~~~~~~~ ^
test.cpp:7:57: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
std::cout << "\"hello\" < \"world\" = " << ("hello" < "world") << std::endl;
~~~~~~~ ^
test.cpp:8:55: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
std::cout << "\"hello\" < \"abc\" = " << ("hello" < "abc") << std::endl;
~~~~~~~ ^
4 warnings generated.
clm2186@athens:~$ ./a.out
"hello" == "world" = 0
"hello" == "hello" = 0
"hello" < "world" = 1
"hello" < "abc" = 1
hello is @ 0x6010e9
hello is @ 0x6010fe
world is @ 0x601113
abc is @ 0x601126
clm2186@athens:~$ g++ -g -Wall test.cpp
test.cpp: In function ‘int main()’:
test.cpp:5:61: warning: comparison with string literal results in unspecified behaviour [-Waddress]
test.cpp:6:61: warning: comparison with string literal results in unspecified behaviour [-Waddress]
test.cpp:7:59: warning: comparison with string literal results in unspecified behaviour [-Waddress]
test.cpp:8:57: warning: comparison with string literal results in unspecified behaviour [-Waddress]
clm2186@athens:~$ ./a.out
"hello" == "world" = 0
"hello" == "hello" = 1
"hello" < "world" = 1
"hello" < "abc" = 1
hello is @ 0x400ad8
hello is @ 0x400ad8
world is @ 0x400ade
abc is @ 0x400af9
#include <iostream>
#include <cstdio>
int main() {
std::cout << "\"hello\" == \"world\" = " << ("hello" == "world") << std::endl;
std::cout << "\"hello\" == \"hello\" = " << ("hello" == "hello") << std::endl;
std::cout << "\"hello\" < \"world\" = " << ("hello" < "world") << std::endl;
std::cout << "\"hello\" < \"abc\" = " << ("hello" < "abc") << std::endl;
printf("hello is @ %p\n", "hello");
printf("hello is @ %p\n", "hello");
printf("world is @ %p\n", "world");
printf("abc is @ %p\n", "abc");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment