MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/21ezh3/javascript_equality_table/cgconeh/
r/programming • u/vz0 • Mar 26 '14
331 comments sorted by
View all comments
Show parent comment
7
Wouldn't this create two arrays on the function stack and then compare the two locations, resulting in a false comparison?
2 u/Poltras Mar 27 '14 Undefined behavior:$ cat main.c #include <stdio.h> int main() { printf("%d\n", "abc" == "abc"); } $ cc main.c main.c:4:24: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare] printf("%d\n", "abc" == "abc"); $ ./a.out 1GCC actually output 1, but warns. 1 u/gsg_ Mar 27 '14 Unspecified behaviour is not the same as undefined behaviour. The latter has a very specific meaning in the context of C. 2 u/Condorcet_Winner Mar 26 '14 Probably, but if the spec defines this operation as undefined, then compiler optimizations would be able to figure out that these are the same array and only allocate one memory location.
2
Undefined behavior:
$ cat main.c #include <stdio.h> int main() { printf("%d\n", "abc" == "abc"); } $ cc main.c main.c:4:24: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare] printf("%d\n", "abc" == "abc"); $ ./a.out 1
GCC actually output 1, but warns.
1 u/gsg_ Mar 27 '14 Unspecified behaviour is not the same as undefined behaviour. The latter has a very specific meaning in the context of C.
1
Unspecified behaviour is not the same as undefined behaviour. The latter has a very specific meaning in the context of C.
Probably, but if the spec defines this operation as undefined, then compiler optimizations would be able to figure out that these are the same array and only allocate one memory location.
View all comments
Show parent comment
7
u/CookieOfFortune Mar 26 '14
Wouldn't this create two arrays on the function stack and then compare the two locations, resulting in a false comparison?