Debugging Seg Fault (GMP lib)

I have a piece of code about 600 lines long which currently doesn't run due to seg fault. I have run gdb but it isn't relly helping me as I don't know what I am looking for. I have also run valgrind which I have been led to believe i more helpful, but I am working on a mac and valgrind doesn't really work on mac. I don't have a linux box and so can't run valgrind properly. Is there anyone willing to run my code through valgrind to find the issue, or are there websites that do this?? The reason I ask here is because I have found websites, but because I am using the gmp bignum library, the website just kicks back error about the library not being there because they don't have it.

Thanks, A.
I was just trying to get GMP to work,..

Did you get a call stack from the debugger (please post)? It can be cryptic if you don't know what you're looking for.
From gdb I get this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000008
0x000000010001ec2d in __gmpf_urandomb ()
(gdb) bt
#0  0x000000010001ec2d in __gmpf_urandomb ()
#1  0x0000000100002013 in main () at TammesA7.cc:60
(gdb) info frame
Stack level 0, frame at 0x7fff5fb61130:
 rip = 0x10001ec2d in __gmpf_urandomb; saved rip 0x100002013
 called by frame at 0x7fff5fbffb30
 Arglist at 0x7fff5fb61128, args: 
 Locals at 0x7fff5fb61128, Previous frame's sp is 0x7fff5fb61130
 Saved registers:
  rbx at 0x7fff5fb61108, r12 at 0x7fff5fb61110, r14 at 0x7fff5fb61118, r15 at 0x7fff5fb61120, rip at 0x7fff5fb61128 


But I have copied the code with mpg_urandomb() into another file and compiled and run that, so I think I am using it right (with regard initialising etc.) so not sure why it is having an issue in this program.

I can post the code if needed, but like I say it is long and messy.
Last edited on
Do you call a function called __gmpf_urandomb() ?

It's where the actual segmentation fault occurs.

I'll soon be done building the GMP lib :)
Yes I do, it is in a loop to assign random numbers (not integers) to floats, but like I said, I tooks this loops and put it in another program and that one runs fine which makes me wonder why it does not in this one...
If that function is a part of the GMP library, it either means:
1. You are using it wrongly (sending the wrong arguments).
or
2. The library itself has a weak spot in its code.

I've installed it, please post the code.
The code can be found here:

http://www.filedropper.com/tammesa7

Note: the loops for n and b can be changed so n goes from. b is essentially just looping the whole code to get many outputs of the same thing.

If you need comments to see what is going on I can add them in, I am fully aware it is a mess at the moment.

Edit: I have tried dropping the precision in the mpf_urandomb() to 128 but this yields no effect.
Last edited on
mpf_set_default_prec(1024);

segfaults for me, on that line. The 5th line from program start. I linked to -lgmp and -lgmpxx, anything else I need to link to?
I compile using:

g++ -o program "TammesA7.cc" -lgmpxx -lgmp -I gmp-5.1.1 -L gmp-5.1.1/cross-tools/lib

So it knows where the library is not just the headers...Might be that?
No if it didn't knew the headers then it wouldn't even compile. Every mpf_ call sigsegvs for me, let me try linking what you've linked.

Edit: still not working, every call segfaults. How weird.
Last edited on
Oh dear...haha, and here I was hoping you could fix my issues...
Well I tried, and I can't get it to work. I'm sorry. All I can say is that your issue is in that urandomb function. Have you looked at the documentation of that function?
Yes I have, and I thought I had it, obviously not. Thanks for trying, help is always appreciated!
For completion, the issue was that the random number generator needs to be initialised. I had forgotten this having been using mpg_class for everything else which initialises for you.

Just add this:
gmp_randinit_default(state);

Before using it.
Topic archived. No new replies allowed.