views:

145

answers:

7

On running the following script, I get segmentation fault. The output consists of "here 5a". But nothing beyond it. Any suggestions on what might be going wrong?

if(model==1) 
  fptr3=fopen("poisson.pro","r");
if(model==2)
  fptr3=fopen("jtt.pro","r");
if(model==3)
  fptr3=fopen("estimate.pro","r"); 

printf ("here 5a\n");
for (i=0;i<20;i++) 
  fscanf(fptr3,"%lf", &freq[i]);
printf ("here 5ai\n");

for (i=0;i<20;i++) 
{
  printf ("here 5b\n");
  for (j=0;j<20;j++) 
  {
    printf ("here 5c\n");
    fscanf(fptr3,"%lf", &prob[i][j]);
    if(model==3) 
      prob[i][j]=prob[i][j]/10000.0;
  }
}

UPDATE

double freq[20], prob[20][20];
A: 

What's freq defined as? What's prob defined as?

Is the first for loop meant to only run fscanf for freq? Or is it supposed to encompass the entire block?

EDIT: Rationale - If all you see is 5a, then it's faulting in the fscanf on freq. You may have allocated too little space for freq, or used an incorrect type.

Matthew Iselin
@Matthew: I have added the definition of freq and prob in the code.
shubster
In light of the edits, daniel is probably on the right track. Are you *absolutely certain* that fptr3 is valid?
Matthew Iselin
+2  A: 

Is this C# ???? wooow no kidding you are getting segfaults... Do you know what return codes are for ??? You really need to improve your code before asking for this kind of help....this is really unreadable, and ugly code... Start adding some check to your file pointers

    if(1==model)
{
 fptr3=fopen("poisson.pro","r");
 if(null == fptr3)
{
  perror("Fopen failed");
}

...

daniel
It's tagged as C, and looking at the code I assume it's not C# ;)
Matthew Iselin
@Matthew: It *was* tagged as C#, but it's obviously C so I retagged it ;)
Sean Nyman
I am sorry guys. Had tagged it c# by mistake.
shubster
+2  A: 

valgrind is the sovereign remedy for segfaults in C. It finds errors before the segfault and tells you exactly what you did wrong. For maximum benefit, compile with debugging symbols turned on.

Norman Ramsey
A: 
  1. Check the return code and print if needed the error code of your files with perror() (see. daniel answer). Calling fscanf() with a FILE * to NULL, will surely do a segfault.

  2. As you specified %lf, freq must be an array of at least 20 doubles, not just float. See man ffrintf().

  3. use a debugger instead of printf().

Ben
After your edits, I suggest you use valgrind as Norman says
Ben
+1  A: 

I agree with Daniel, you need to be checking your return values and pointers.

I also agree that you need to do a better job blocking and formatting your code. Supposing that you are the only one that ever reads it, in a couple of weeks even you will have forgotten enough that you won't be able to follow it.

Towards Matthews point, you could try deleting everything starting with the line:

printf ("here 5ai\n");

Of course, only bother with this after checking the file pointer.

Another crazy idea would be stepping through it with a debugger. I know it's hard to use a debugger when you have such great debugging options as the printf statement, but sometimes a debugger can be a huge time saver (for example, when you keep getting segfault). Also, learning how to use the debugger for the platform you are developing on is one of the things that separates good coders from the pack of average hacks. (FYI, you could even try looking at your core in the debugger and maybe see exactly where the problem is. (Hint: if you don't have a core file try 'man ulimit'.))

If you are going to insist on using printf as your only means of debug, then call flush right after each use.

Mike
A: 

You should post the contents of the beginning of whatever file is getting opened since it's crashing in fscanf. Either the file doesn't exist, or it's malformed when stacked against to how you are trying to parse it.

Jim Buck
+2  A: 

Are you sure this code is called and "model" is either 1, 2, or 3? If not, fptr3 would not be set, so if you try to do anything with it you'd be in trouble.

Try this instead?:

void modeltest (int model) 
{
    FILE *fptr3 = NULL;
    int i = 0;
    int j = 0;
    double freq[20], prob[20][20];

    if(model==1) fptr3=fopen("poisson.pro","r");
    if(model==2) fptr3=fopen("jtt.pro","r");
    if(model==3) fptr3=fopen("estimate.pro","r");

    printf ("here 5a\n");
    if (fptr3 != NULL) {
     for (i=0;i<20;i++) fscanf(fptr3,"%lf", &freq[i]);
     printf ("here 5ai\n");
     for (i=0;i<20;i++) {
      printf ("here 5b\n");
      for (j=0;j<20;j++) {
       printf ("here 5c\n");
       fscanf(fptr3,"%lf", &prob[i][j]);
       if(model==3) prob[i][j]=prob[i][j]/10000.0;
      }
     }
    }
    else {
     printf ("fptr3 is NULL!\n");
    }
}

An additonal note. Please, please, please consider consistent brace styles! :)

BenB