Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segfault in std::atomic load?

On linux, using gcc 4.8.4, compiled with -std=c++11 -mcx16:

#include <atomic>

struct node_t;

struct pointer_t {
        node_t* ptr;
        unsigned int count;
        pointer_t() noexcept : ptr{nullptr}, count{0} {}
};

struct empty {};

struct node_t {
        empty value;
        std::atomic<pointer_t> next;
        node_t() : next{pointer_t{}} {}
};

int main() {
        node_t{}.next.load();
        return 0;
}

gives a segfault when load is called. How am I meant to initialize an atomic value?

like image 985
Shea Levy Avatar asked Apr 23 '15 13:04

Shea Levy


People also ask

What does STD atomic load do?

std::atomic<T>::load Atomically loads and returns the current value of the atomic variable. Memory is affected according to the value of order .

How is Segfault caused?

In practice, segfaults are almost always due to trying to read or write a non-existent array element, not properly defining a pointer before using it, or (in C programs) accidentally using a variable's value as an address (see the scanf example below).


1 Answers

Turns out this is a bug in gcc that has since been fixed in GCC 5.1. Specifying the alignment to be two words fixed it.

like image 198
Shea Levy Avatar answered Oct 06 '22 00:10

Shea Levy