Page 1 of 1

drand48 give segfault

Posted: Wed Jan 13, 2016 6:07 am
by useruseruser
Hi,

I'm trying to use the function drand48, but when I call it, it causes a segmentation fault.

I do put in the top of my source file:

Code: Select all

#define _XOPEN_SOURCE
#include <stdlib.h>
And I do call

Code: Select all

srand48(long_int_var);
before calling drand48.

Calling the function rand (after srand) is also causing segmentation fault.

I'm starting to think it's a problem with the Raspberry Pi, or with my version of the C standard lib.

Re: drand48 give segfault

Posted: Wed Jan 13, 2016 7:52 am
by AndyD
It works for me using both gcc (4.9.2) and clang (3.5.0+rp1) using the following code.

Code: Select all

#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    long int seed = 0xDEADBEEF;

    srand48(seed);

    printf("%f\n", drand48());

    return 0;
}

Re: drand48 give segfault

Posted: Wed Jan 13, 2016 8:37 am
by jahboater
Works with gcc 5.3 on a Pi2.
Is your program multi-threaded by any chance?

Re: drand48 give segfault

Posted: Wed Jan 13, 2016 4:12 pm
by useruseruser
Thank you both.

Yes, it is multi-threaded ATM. However, I'll have to remove the threads anyway. Does that have anything to do with it? I mean, I know srand48 has to be called for every thread, but this segfault occurs in the main thread, before any other thread is created.

Re: drand48 give segfault

Posted: Wed Jan 13, 2016 5:33 pm
by useruseruser
I just removed the threads and now it works! TY! :)

Re: drand48 give segfault

Posted: Wed Jan 13, 2016 9:14 pm
by jahboater
Most pseudo rng's save the state between calls in static memory.
Glad it works now!