useruseruser
Posts: 51
Joined: Fri Aug 01, 2014 11:34 pm

drand48 give segfault

Wed Jan 13, 2016 6:07 am

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.

User avatar
AndyD
Posts: 2333
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: drand48 give segfault

Wed Jan 13, 2016 7:52 am

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;
}

jahboater
Posts: 4846
Joined: Wed Feb 04, 2015 6:38 pm

Re: drand48 give segfault

Wed Jan 13, 2016 8:37 am

Works with gcc 5.3 on a Pi2.
Is your program multi-threaded by any chance?

useruseruser
Posts: 51
Joined: Fri Aug 01, 2014 11:34 pm

Re: drand48 give segfault

Wed Jan 13, 2016 4:12 pm

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.

useruseruser
Posts: 51
Joined: Fri Aug 01, 2014 11:34 pm

Re: drand48 give segfault

Wed Jan 13, 2016 5:33 pm

I just removed the threads and now it works! TY! :)

jahboater
Posts: 4846
Joined: Wed Feb 04, 2015 6:38 pm

Re: drand48 give segfault

Wed Jan 13, 2016 9:14 pm

Most pseudo rng's save the state between calls in static memory.
Glad it works now!

Return to “C/C++”