Search code examples
c++segmentation-faultgmp

Seg Fault 11, (issue with GMP and initialisation??)


I have a segmentation fault 11 in this piece of code (edit:no longer shown).

Having been told to get a debugger and to sort my arrays out I have edited the code to fix the arrays to go from 0. I also installed valgrind and ran it but it is limited when working on OSX. I have run gbd and the issue is described below.

If this just looks horrendously ominous, or the code is needed to make any kind of headway, let me know and I can edit the question to the format the forum expects.

Running gdb on my code I get:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5e4c27c8
0x0000000100001048 in main () at TammesA3.cc:23
23    mpf_set_default_prec(1024);

The code is as follows and the issue is caused before my loops later on even start!

#include <stdio.h> 
#include <string.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <sstream>
#include <gmpxx.h>
using namespace std;
#define PI 3.14159265358979323846

mpf_t E[100000];

int main()
{

  int a,b,c,d,f,i,j,k,m,n,s,Success,Fails;
  double p,theta,phi,Time,Averagetime,Distance,Length,DotProdForce,
         Forcemagnitude,ForceMagnitude[200],Force[200][3],Epsilon[3],
         x[200][3],new_x[200][3],y[200][3],A[200],alpha[200][200],degree,bestalpha[500];
  unsigned long int t;

  mpf_set_default_prec(1024);

  mpf_t Energy,energy,Power,D,En[500],Ep,e;

  mpf_init(Power);
  mpf_init(D);
  mpf_init(Ep);
  mpf_init(e);
  mpf_init(Energy);
  mpf_init(energy);

  for(i=0;i<100000;i++){
   mpf_init(E[i]);
  }

  for(i=0;i<500;i++){
    mpf_init(En[i]);
  }

  mpf_set_d(e,0.00000000000000000000000000000000000000000000000000000000001);

  clock_t t1,t2;
  t1=clock();

t=1000;

while(t<1001){

n=20;

while(n<21){

cout << "N=" << n << "\n";

  b=1;
  Time=0.0;

  while(b<2){

    clock_t t3,t4;
    t3=clock();

    if(n>200){
      cout << n << " is too many points for me :-( \n";
      exit(0);
    }

    srand((unsigned)time(0));  

    for (i=0;i<n;i++){
      x[i][0]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
      x[i][1]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
      x[i][2]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;

      Length=sqrt(pow(x[i][0],2)+pow(x[i][1],2)+pow(x[i][2],2));

      for (k=0;k<3;k++){
        x[i][k]=x[i][k]/Length;
      }
    }

    for(i=0;i<n;i++){
      for(j=0;j<3;j++){
        cout << "x[" << i << "][" << j << "]=" << x[i][j] << "\n";
      }
    }

    /* Points distributed randomly and normalised so they sit on unit sphere */

    mpf_set(Energy,0);

    for(i=0;i<n;i++){
      for(j=i+1;j<n;j++){
        Distance=sqrt(pow(x[i][0]-x[j][0],2)+pow(x[i][1]-x[j][1],2)
                     +pow(x[i][2]-x[j][2],2));

        mpf_set_d(D,Distance);

        mpf_pow_ui(Power,D,t);      
        mpf_ui_div(Power,1.0,Power);
        mpf_add(Energy,Energy,Power);

      }
    } etc.

after this, my code goes on into a loop, but the code as said in comments in really long so shan't put it in again unless requested.


Solution

  • The remaining problem(1) is (on three lines)

    mpf_set(Energy,0);
    

    Its prototype is

    void mpf_set (mpf_t ROP, mpf_t OP)
    

    and an mpf_t is an array,

    typedef __mpf_struct mpf_t[1];
    

    so you pass a null pointer for the value to read. Dereferencing a null pointer is another typical cause of segmentation faults (besides accessing beyond array bounds).

    You meant to use mpf_set_d, mpf_set_ui or mpf_set_si there (which one you use doesn't matter).


    (1) I have copied the code from revision 2 and moved the two big arrays E and alpha to file scope to relieve the stack of that burden, after that and fixing three mpf_set(variable,0) on lines 94, 147 and 271, it ran to completion.

    That doesn't necessarily mean the code is correct, at the end of the output I read

    Successes=0 Failures=1
    Success Rate=0.00000
    

    which hints at a possible problem in the code logic, but I don't know what output to expect.