Сообщение [UPD] забавный крэш от 30.09.2021 20:15
Изменено 30.09.2021 20:50 ononim
#include <string>
#include <iostream>
bool foobar(const std::string &s)
{
std::string sx = s;
std::cout << sx << std::endl;
}
int main(int argc, char **argv)
{
foobar(argv[0]);
return 0;
}
.. а если поменять bool foobar на void foobar, или добавить return false, то не падает. Ну подумаешь забыл вернуть bool/home/user/test$ g++ -O2 -std=c++11 ./test.cpp -o ./test && ./test
./test.cpp: In function 'bool foobar(const string&)':
./test.cpp:8:1: warning: no return statement in function returning non-void [-Wreturn-type]
8 | }
| ^
./test
Segmentation fault (core dumped)
С GCC 7.5.0 не падает кстати.
[UPD]
std::string как выяснилось непричем, чисто сишный аналог так же падает, будучи собранным g++:
#include <stdio.h>
bool foobar(const char *s)
{
printf("foobar(%s)\n", s);
}
int main(int argc, char **argv)
{
foobar(argv[0]);
return 0;
}
/home/user/test$ g++ -O2 ./test.c -o ./test && ./test
./test.c: In function 'int foobar(const char*)':
./test.c:6:1: warning: no return statement in function returning non-void [-Wreturn-type]
6 | }
| ^
foobar(./test)
Segmentation fault (core dumped)
а вот gcc -O2 ./test.c -o ./test && ./test — бодрячком
#include <string>
#include <iostream>
bool foobar(const std::string &s)
{
std::string sx = s;
std::cout << sx << std::endl;
}
int main(int argc, char **argv)
{
foobar(argv[0]);
return 0;
}
.. а если поменять bool foobar на void foobar, или добавить return false, то не падает. Ну подумаешь забыл вернуть bool/home/user/test$ g++ -O2 -std=c++11 ./test.cpp -o ./test && ./test
./test.cpp: In function 'bool foobar(const string&)':
./test.cpp:8:1: warning: no return statement in function returning non-void [-Wreturn-type]
8 | }
| ^
./test
Segmentation fault (core dumped)
С GCC 7.5.0 не падает кстати.
[UPD]
std::string как выяснилось непричем, чисто сишный аналог так же падает, будучи собранным g++:
#include <stdio.h>
bool foobar(const char *s)
{
printf("foobar(%s)\n", s);
}
int main(int argc, char **argv)
{
foobar(argv[0]);
return 0;
}
/home/user/test$ g++ -O2 ./test.c -o ./test && ./test
./test.c: In function 'int foobar(const char*)':
./test.c:6:1: warning: no return statement in function returning non-void [-Wreturn-type]
6 | }
| ^
foobar(./test)
Segmentation fault (core dumped)
а вот gcc -O2 ./test.c -o ./test && ./test — бодрячком
Ну я понимаю конечно что undefined behaviour, но зачем прям сразу начисто ногу отстреливать то