std::ifstream doesn't work with -O3 optimization - c++

I am trying to run this code with optimization key -O3 (-O1 and O2 also do not work)
main.cpp:
#include "my_file.hpp"
int main(int argc, char* argv[]){
test_another();
return 0;
}
my_file.hpp:
#include <string>
#include <fstream>
void test_another(){
std::ifstream bar;
bar.open("C:\\Users\\ische\\source\\repos\\OpenMpCpp\\test.in");
}
What I get: "Exception has occurred. Segmentation fault" in istream file when basic_istream() is called.
If function test_another() is moved to the main.cpp file - all works fine even with -O3 key. If program is compiled without any optimization key (O1, O2, O3) then it is a successful compilation.
I use Windows10, compiler is g++ from MinGW package: g++.exe (MinGW.org GCC-6.3.0-1) 6.3.0
Anyone has any ideas why is this happening?

Related

g++ -std=c++17 gives lines and lines of errors

I am trying to compile this code with MinGW g++ (i686-win32-dwarf-rev0, Built by MinGW-W64 project) 8.1.0
#include <bits/stdc++.h>
using namespace std;
int main()
{
map<int, int> mmap;
mmap[0]=10;
mmap[1]=20;
mmap[2]=30;
mmap[3]=40;
mmap[4]=50;
for(auto [x,y]:mmap){
cout<<x<<"->"<<y<<endl;
}
return 0;
}
Compiling with c++11 flag gives this
E:\Code>g++ temp.cpp -std=c++11
temp.cpp: In function 'int main()':
temp.cpp:89:14: warning: structured bindings only available with -std=c++17 or -std=gnu++17
for(auto [x,y]:mmap){
and compiling with c++17 flag gives lines and lines of errors.
g++ temp.cpp -std=c++17
OK, so I figured this out and this came out to be the very first line
#include <bits/stdc++.h>
Including iostream and map instead of the above line results in a clean compilation.
#include<iostream>
#include<map>
Now I have one more reason as to Why should I not #include <bits/stdc++.h>?

Why does the memory-sanitizer report use of an uninitialized value for std::map?

I'm using manjaro linux on x86-64. Memory-sanitizer in clang version 10.0.1 reported a use of uninitialized value error in std::map, which quite surprised me. Did I do something wrong?
$ cat test.cpp
#include <map>
int main() {
std::map<int, int> test;
test.insert({1,2});
}
$ clang++ -fsanitize=memory test.cpp && ./a.out
==51936==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x562889eaad9a (/tmp/build/a.out+0x9fd9a)
#1 0x562889eaae28 (/tmp/build/a.out+0x9fe28)
#2 0x562889eaaba1 (/tmp/build/a.out+0x9fba1)
#3 0x562889eaa51e (/tmp/build/a.out+0x9f51e)
#4 0x562889eaa087 (/tmp/build/a.out+0x9f087)
#5 0x7f418e02b151 (/usr/lib/libc.so.6+0x28151)
#6 0x562889e2b1dd (/tmp/build/a.out+0x201dd)
SUMMARY: MemorySanitizer: use-of-uninitialized-value (/tmp/build/a.out+0x9fd9a)
Exiting
FWIW it looks like libc++ is more MSAN-friendly than stdlibc++ because compiling a similar
#include <map>
#include <string>
int main(int argc, char** argv) {
std::map<int, std::string> m;
m[argc] = argv[argc - 1];
return 0;
}
code with the latter and running
% clang++ -fsanitize=memory -fno-omit-frame-pointer -g -O2 umr.cpp
results in a similar error, but doing
% clang++ -fsanitize=memory -fno-omit-frame-pointer -stdlib=libc++ -g -O2 umr.cpp && ./a.out
works fine (clang 13, Debian Sid).
When using MemorySanitizer, all libraries you use must be compiled with MemorySanitizer. Otherwise, there is a risk of false positives. This includes the C++ standard library itself.
You will find instructions for compiling libc++ with MemorySanitizer in the official sanitizers wiki:
https://github.com/google/sanitizers/wiki/MemorySanitizerLibcxxHowTo

segmentation fault using static libraries with std::jthread (g++-10)

I'm developing a library that works with std::jthread (new in C++20) using g++ 10.0.1. The library works fine if I compile it using share libraries, but If I compile it with static libraries I got a segmentation fault at the end of the program (thread destructor). I have narrowed down my test case to a simple thread creation and join:
#include <iostream>
#include <thread>
int main(int argc, const char** argv) {
auto t = std::jthread([]() { std::cout << "OK\n"; });
t.join();
return 0;
}
To compile I use:
g++-10 -o test --static -std=c++20 test.cc -lpthread
And running it:
% ./test
zsh: segmentation fault (core dumped) ./test
There any one has an idea what this problem could be?
Update:
Following #JérômeRichard suggested reference, I was able to compile and run my little test program without problem
g++-10 -o test --static -std=c++20 test.cc -lrt -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive
However, changing the code to use request_stop instead of join the program is segmenting fault again (https://godbolt.org/z/obGN8Y).
#include <iostream>
#include <thread>
int main() {
auto t = std::jthread([]{ std::cout << "OK" << std::endl; });
t.request_stop();
}
The problem was reported to libstdc++ (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95989) and a patch has been generated to solve the problem.

std:thread on macOS: libc++abi.dylib: terminating

While running the following very simple code
#include <thread>
#include <iostream>
using namespace std;
void test() {cout << "hello" << endl;}
int main(){
thread t(test);
t.join();
}
And compiling with
g++ -std=c++14 -pthread multi.cc
on macOS10.12.6 with clang 900.0.38, the program shows
libc++abi.dylib: terminating Abort trap: 6
Does anyone know what might be causing this behaviour?
Don’t use -pthread option, as you are using std::thread
Compile with below command:
g++ -std=c++14 multi.cc -o multi

C++ math.h on OS X 10.8.5

I have a C++ program that will not compile under OS X 10.8.5 with the g++ compiler. The problem seems to be with the math.h header file.
This is the version of g++ is
g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/Users/densmore3/local/usr/local/bin/../libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/lto-wrapper
Target: x86_64-apple-darwin14.0.0
Configured with: ../gcc-4.9-20141029/configure --enable- languages=c++,fortran
Thread model: posix
gcc version 4.9.2 20141029 (prerelease) (GCC)
There are 40-50 errors of the type below. The code compiled fine on 10.6. What is going on?
/Users/xxxx/local/usr/local/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/include-fixed/math.h:203:1: error: ‘__header_always_inline’ does not name a type
__header_always_inline int __inline_isfinitef(float);
Users/densmore3/local/usr/local/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/include-fixed/math.h:580:27: error: expected initializer before ‘__AVAILABILITY_INTERNAL__MAC_10_0_DEP__MAC_10_9’
extern float __inff(void) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_9, __IPHONE_NA, __IPHONE_NA);
Here is a piece of test code that gives the same error as my real code. The error goes away if I remove the math.h include statement.
#include <iostream>
#include <math.h>
#include <stdio.h>
//#include <complex>
//#include <vector>
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
The compile command I am using is:
g++ test.cpp
gcc-4.8 is the correct version to use on OS X 10.8.