Take the 2-minute tour ×

Iv'e been experimenting with the c++11 random library, and running into a strange problem. My code compiles fine and runs inside Eclipse, but as soon as I try to run the executable in a shell (bash on linux in this case), I get a core dump. I checked if I had any special environment variables in Eclipse, but this doesn't seem to be the case.

My humble code:

std::mt19937 eng(time(NULL));
std::uniform_int_distribution<int> uniformEnergy(0,E.size()-1);
std::uniform_real_distribution<double> uniformProb(0,0.1);
    int E_r = uniformEnergy(eng);
    float p_r = uniformProb(eng);

And the seg fault, with a seemingly infinite (I gave up after 4000) stack inside random.h:

#0  0x0000000000403af4 in std::uniform_int_distribution<int>::param_type::b (
this=<error reading variable: Cannot access memory at address 0x7fffc1381ff8>) 
at /usr/include/c++/4.6/bits/random.h:1636 
1636        b() const

What could be so wrong in my terminal, yet work so nicely in Eclipse?

Edit: I tried compiling the code myself, and it works fine, leading me to believe the the executable made by eclipse is bad for some reason. Any ideas? Edit 2 This s of course a dumb error on my part: the E.size() was different depending on the directory I was running in, leading (probably) to it being zero.


share|improve this question
where is the rest of the code? –  BЈовић Aug 29 '12 at 7:41
Could you post a minimum example to illustrate this? –  user1202136 Aug 29 '12 at 7:43
Probably you try to access a temporary object somewhere that has already been removed (i.e. a reference to a temporary variable on the stack), and it works in Eclipse only by chance. E.g.: How is E constructed? –  JohnB Aug 29 '12 at 7:47
Random lib is not thread safe. just a hunch –  sehe Aug 29 '12 at 7:48
No, you most probably have an error in your code that becomes apparent randomly. –  JohnB Aug 29 '12 at 8:15
show 1 more comment


Your Answer



Browse other questions tagged or ask your own question.