I have the following simple code:
// test.hpp
#pragma once
#include <deque>
struct A {
static thread_local std::deque<int> g;
};
// test.cpp
#include "test.hpp"
thread_local std::deque<int> A::g = std::deque<int>();
// main.cpp
#include "test.hpp"
int main() {
A::g.emplace_back(0);
return 0;
}
When I compile it with Clang, it finished with code 0 and everything is OK. However, if I compile it with GCC, it crashes with SIGSEGV.
A bit of debugging into the GCC STL source code showed me that during emplace_back
, the internal pointers of the std::deque
seem to be NULL
, which causes the failure on an attempt to allocate memory and place the int
into it.
Am I doing something wrong, or is this a GCC bug?
Versions:
- Clang (which produces a working binary): Homebrew clang version 16.0.2
- GCC (which produces a crashing binary): g++-13 (Homebrew GCC 13.1.0) 13.1.0
- OS: macOS Monterey 12.5.1
P.S. Replacing the std::deque with std::vector somehow fixes the problem (GCC-produces binary finishes with code 0)
UPD:
clang compilation:
clang++ -std=c++20 main.cpp test.cpp -o run
gcc compilation (both produced executables fail):
g++-13 -std=c++20 main.cpp test.cpp -o run
or
g++-13 -std=c++20 -pthread -c test.cpp main.cpp && g++-13 -pthread test.o main.o -o run
The failure message I am getting:
[1] 95879 segmentation fault ./run