|
initstate_r in C crashing
|
Reply to this subject
Start a new subject
 
Refresh page
| Posted by |
Zeno
USA (2,867 posts) bio
Moderator |
| Date |
Sun 26 Jun 2011 07:50 PM (UTC) [ quote
] |
| Message |
I am getting a crash when trying to use initstate_r:
Quote: (gdb) run
Starting program: /home/user/test.out
Program received signal SIGSEGV, Segmentation fault.
0x40052d00 in initstate_r () from /lib/libc.so.6
The code:
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STATELEN 256 /* random number state buffer */
main()
{
char randomStateBuffer[STATELEN];
struct random_data randData;
printf("Before initstate");
/* seed the random number generator */
initstate_r (time(NULL), (char *)&randomStateBuffer, STATELEN,
(struct random_data *)&randData);
printf("initstate done");
}
I asked this on SO a while ago but it wasn't solved: http://stackoverflow.com/questions/4167034/c-initstate-r-crashing |
Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org | top |
|
| Posted by |
Twisol
USA (2,229 posts) bio
|
| Date |
Reply #1 on Sun 26 Jun 2011 08:20 PM (UTC) [ quote
] Amended on Sun 26 Jun 2011 08:21 PM (UTC) by Twisol
|
| Message |
randomStateBuffer is already a char*, so using & on it results in char**. I know that was mentioned in an answer on SO, but it should definitely be changed.
I'm pretty sure you don't need the (struct random_data*) cast either, because it already is a (struct random_data*) after you use &. Not something that would make things break though.
I honestly don't see anything else that would make it crash. A post on a mailing list [1] suggests that, oddly enough, you need to do this before calling initstate_r():
randData.state = (int32_t*)randomStateBuffer;
[1]: http://lists.debian.org/debian-glibc/2005/08/msg00492.html |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | top |
|
| Posted by |
Twisol
USA (2,229 posts) bio
|
| Date |
Reply #2 on Sun 26 Jun 2011 08:33 PM (UTC) [ quote
] |
| Message |
Actually, this code seems to compile and run without segfaulting. Apparently you need to zero out the random_data structure first.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct random_data random_data;
int main() {
char statebuf[256];
random_data randomData;
memset(&randomData, 0, sizeof(randomData));
initstate_r(0, statebuf, 256, &randomData);
return 0;
}
|
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | top |
|
| Posted by |
Zeno
USA (2,867 posts) bio
Moderator |
| Date |
Reply #3 on Sun 26 Jun 2011 09:05 PM (UTC) [ quote
] |
| Message |
That code still crashes for me. :(
(gdb) run
Starting program: /home/p4r/a.out
Program received signal SIGSEGV, Segmentation fault.
0x40052c33 in initstate_r () from /lib/libc.so.6
(gdb) bt
#0 0x40052c33 in initstate_r () from /lib/libc.so.6
#1 0x080483f8 in main ()
|
Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org | top |
|
| Posted by |
Twisol
USA (2,229 posts) bio
|
| Date |
Reply #4 on Sun 26 Jun 2011 09:26 PM (UTC) [ quote
] |
| Message |
What command are you using to compile? I have `gcc -o foo foo.c`. I'm using GCC 4.4.5.
What OS? I'm on Ubuntu 10.10. |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | top |
|
| Posted by |
Zeno
USA (2,867 posts) bio
Moderator |
| Date |
Reply #5 on Sun 26 Jun 2011 09:32 PM (UTC) [ quote
] Amended on Sun 26 Jun 2011 09:37 PM (UTC) by Zeno
|
| Message |
p4r@zeno:~$ gcc -o test test.c
p4r@zeno:~$ gdb test
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i486-slackware-linux"...Using host libthread_db library "/lib/libthread_db.so.1".
(gdb) run
Starting program: /home/p4r/test
Program received signal SIGSEGV, Segmentation fault.
0x40052c33 in initstate_r () from /lib/libc.so.6
(gdb) bt
#0 0x40052c33 in initstate_r () from /lib/libc.so.6
#1 0x080483f8 in main ()
(gdb)
I'm on Slackware 10.2.0
Old yeah, but I've tried upgrading libc and it didn't fix this crash. |
Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org | top |
|
| Posted by |
Nick Gammon
Australia (18,770 posts) bio
Forum Administrator |
| Date |
Reply #6 on Sun 26 Jun 2011 10:02 PM (UTC) [ quote
] |
| Message |
| Show us your current code Zeno? |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
| Posted by |
Nick Gammon
Australia (18,770 posts) bio
Forum Administrator |
| Date |
Reply #7 on Sun 26 Jun 2011 10:03 PM (UTC) [ quote
] |
| Message |
| You shouldn't need to cast something simple like that. Casting and then getting a crash tends to indicate a bad cast. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
| Posted by |
Zeno
USA (2,867 posts) bio
Moderator |
| Date |
Reply #8 on Sun 26 Jun 2011 10:06 PM (UTC) [ quote
] Amended on Sun 26 Jun 2011 11:16 PM (UTC) by Zeno
|
| Message |
Twisol's code is what I tried, still crashed. Code I tried originally is in the OP.
Oh yeah, valgrind. Here's what I get: http://pastebin.com/w7qcRgnr |
Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org | top |
|
| Posted by |
Nick Gammon
Australia (18,770 posts) bio
Forum Administrator |
| Date |
Reply #9 on Mon 27 Jun 2011 12:36 AM (UTC) [ quote
] Amended on Mon 27 Jun 2011 12:37 AM (UTC) by Nick Gammon
|
| Message |
Hmm. Try:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
char statebuf[256];
struct random_data randomData;
memset(&randomData, 0, sizeof(randomData));
initstate_r(0, statebuf, sizeof(statebuf), &randomData);
printf("initstate done\n");
return 0;
}
If that doesn't work, not sure. Try the Marsenne Twister maybe? |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
1,520 views.
Reply to this subject
Start a new subject
 
Refresh page
top
Comments to:
Gammon Software support
Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )