GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Oct-2006, 20:45
jlwelch jlwelch is offline
New Member
 
Join Date: Oct 2006
Posts: 13
jlwelch is on a distinguished road

Find the Segmentation Fault


Hello again. It appears I have reached another snag. The following code compiles just fine, but continuously gives me a segmentation fault. I suspect that something may not have gone well with the MemAlloc1 function.

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{

  /* Prototype Declarations */

  int NumRows();              /*  A function that determines n            */
  float **MemAlloc1(int n);   /*  A function to allocate a 2D array       */
  void Import1(int n, float **data1);

  /* Local Definitions */

  int n;                      /*  n is the number of rows in a datafile   */

  float **data1;              /*  A pointer to the array meant for file1  */
  
/* Statements*/

  n = NumRows();

  data1 = MemAlloc1(n);
  Import1(n, data1);

  return(0);
}

int NumRows()
{
  /* Local Definitions */

  int n;                      /*  n is the number of rows in a datafile   */
  float i;                    /*  A placeholder                           */
  float j;                    /*  A placeholder                           */
  float k;                    /*  A placeholder                           */
  float l;                    /*  A placeholder                           */

  FILE *fpdata;               /*  The datafile itself to be read          */

  /* Statements */

  fpdata = fopen("datafile","r");

  n = 0;
  while(fscanf(fpdata, "%f %f %f %f", &i, &j, &k, &l) != EOF)
  {
    n = n + 1;
  }

  fclose(fpdata);
  return(n);
}

float **MemAlloc1(int n)
{
  /* Local Definitions */

    int i;                    /*  A counter to end the loop appropriately */
    float **data1;            /*  A pointer to the array we are building  */

  /* Statements */

    data1 = (float **)malloc(sizeof(float*));
    for(i = 0; i < 4; i++)
    {
      data1[i] = (float *)malloc(n);
    }

    return(data1);
}

void Import1(int n, float **data1)
{
  /* Local Definitions */

  FILE *fpdata;

  int i;
  int j;
  int k;
  int l;
  int m;

  /* Statements */

  fpdata = fopen("datafile","r");
  m = 0;

  while(fscanf(fpdata, "%f %f %f %f",i,j,k,l) != EOF)
  {
    data1[0][m] = i;
    data1[1][m] = j;
    data1[2][m] = k;
    data1[3][m] = l;

    printf("%f %f %f %f\n", data1[0][m],data1[1][m],data1[2][m],data1[3][m]);
    m++;
  }

  fclose(fpdata);
  return;
}

I apologize if this looks like a daunting amount of information, but if you can help me fix the Segmentation Fault, I would greatly appreciate it.
Last edited by LuciWiz : 11-Oct-2006 at 00:36. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 09-Oct-2006, 22:17
davis
 
Posts: n/a

Re: Find the Segmentation Fault


Whatever you do, definitely DO NOT read the F&%#ing www.gidforums.com.

However, you may want to CHECK to see if the FILE POINTER is valid before you USE IT.

Take a look at the value of fpdata as you STEP THROUGH your program with a symbolic debugger.

I'm guessing that your file pointer is invalid.


:davis:
  #3  
Old 09-Oct-2006, 22:21
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Find the Segmentation Fault


Quote:
Originally Posted by jlwelch
continuously gives me a segmentation fault.

CPP / C++ / C Code:
    while (fscanf(fpdata, "%f %f %f %f", i, j, k, l) != EOF) {

1. WIth scanf, you must give the addresses of the variables where the data are going to be stored. Using a variable instead of its address can definitely cause a segment fault.

2. If you are going to store data into int variables, use %d, not %f. On the other hand if they are supposed to be floats, then declare the variables as floats, not ints. This wouldn't cause a seg fault, as far as I can tell, but it definitely gets the wrong stuff into your program.

3. I would test the return value of fscanf against the number of items that I expected to convert, rather than EOF. This somewhat a matter of style, but I prefer it as it not only allows you to detect the end of file, but also allows the program to quit trying to read in case of invalid input data. (But your method is valid, and in any case shouldn't cause a seg fault.)

CPP / C++ / C Code:
    float i;
    float j;
    float k;
    float l;
.
.
.
    while (fscanf(fpdata, "%f %f %f %f", &i, &j, &k, &l) == 4) {


4. When you try to open a file, make sure it's open before trying to access it:

CPP / C++ / C Code:
    printf("In NumRows()\n");
    fpdata = fopen("datafile", "r");
    if (!fpdata) {
        printf("There was a problem opening the file for reading\n");
        exit(1);
    }
    printf("Opened file for reading\n");


5. Finally, your allocation stuff is wrong (as you suspected). This might or might not lead to seg faults with some data files (depending on the sizes), but is definitely a bug.

According to your usage, if there are n lines of data in the file and each line has four floats:

You should allocate enough memory for 4 pointers. Each pointer will point to n floats, so inside the loop you should allocate that much storage for each of the four pointers. (The argument to malloc is the number of bytes. So to allocate enough storage for n pointers, you multiply the size of a pointer by n. That's (probably) not the notation I would have used but that's what you have in Import1().

CPP / C++ / C Code:
    data1 = malloc(4 * sizeof(float *)); /* will have 4 pointers */
    for (i = 0; i < 4; i++) {
        data1[i] = malloc(n * sizeof(float)); /* each points to n floats */
    }


If you still get seg faults, you can try putting printf statements just before and after (and inside) function calls. When you can't see the "obvious" problem, sometimes this allows you to zero in on the problem. (Or, you can use a debugger if one is available. A debugger like gdb can sometimes tell you exactly where the problem is.)

like:

CPP / C++ / C Code:
    printf("Calling NumRows()\n");
    n = NumRows();
    printf("Back from NumRows(), n = %d\n", n);


Stuff like that helps you know how far your program is getting before the big stinkbomb.


Regards,

Dave
Last edited by davekw7x : 09-Oct-2006 at 23:18.
  #4  
Old 10-Oct-2006, 10:16
jlwelch jlwelch is offline
New Member
 
Join Date: Oct 2006
Posts: 13
jlwelch is on a distinguished road

Re: Find the Segmentation Fault


Thank you Dave, you have once again successfully solved my problem! And thanks to everyone else who suggested helpful ideas, you guys are great!

 
 

Recent GIDBlogPh.D. progress by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Another Segmentation Fault Problem SergeDrago C++ Forum 2 30-Nov-2005 14:16
segmentation fault? in C micheldenostra C Programming Language 1 10-Sep-2005 12:27
Please help segmentation fault problem robsmith C Programming Language 1 08-May-2005 20:34
segmentation fault in c++ rushman8282 C++ Forum 2 26-Jan-2005 03:38

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:42.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.