[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  initstate_r in C crashing
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

initstate_r in C crashing

It is now over 60 days since the last post. This thread is closed.   [New subject]  Start a new subject   [Refresh] Refresh page


Posted by Zeno   USA  (2,871 posts)  [Biography] 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
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] 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
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] 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
[Go to top] top

Posted by Zeno   USA  (2,871 posts)  [Biography] 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
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] 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
[Go to top] top

Posted by Zeno   USA  (2,871 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (19,617 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (19,617 posts)  [Biography] 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
[Go to top] top

Posted by Zeno   USA  (2,871 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (19,617 posts)  [Biography] 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
[Go to top] 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.


3,427 views.

It is now over 60 days since the last post. This thread is closed.   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Web site powered by FutureQuest.Net]