Hi I am writing a program in C, however I am getting a segmentation fault upon running my program.
I am using gcc to compile and no warnings or errors are given at compilation time.
I tried using gdb to trace the origin of the segfault and it directs me to the line where I am assigning data values to my two dimensional array: array[row][column] = datavalue;
When I run my program stores 3 data values and then seg faults. It should store data on 424 rows by 117 columns, but consistently it seg faults after only storing 3 data values.
My code is as follows (with some details left out):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void allmem(float** fpdata,...); // allocate memory header
void stored(float** fpdata,...); // store data header
void countnumber(int* num1, int*num2,...); // count header
int main() // main() function
{
int numberofrows = 424; // number of rows
float** fpdata; // two dimensional array fpdata of floats
allmem(fpdata,...); // call allocate memory function
stored(fpdata,...); // call function to store data
...
return 0;
} // main ()
// --------------stored() function---------------
void stored(float** fpreal,...) {
FILE *fp; // file pointer
float datavalue; // variable to hold data values read
int row;
int column;
fp = fopen("c:/file.txt","r");
for(column = 0; column < 117; column++) {
for(row = 0; row < 424; row++) {
fscanf(fp, "%f,", &datavalue);
fpdata[row][column] = datavalue;
} // for
} // for
fclose(fp);
} // stored()
// ----------allmem() function----------------
// function to allocate memory for two dimensional arrays
// I have hard coded the values of the array sizes in, but
// in my actual program they are calculated at run time based
// on the size of some input files and this is done in the
// countnumber() function
void allmem(float** fpdata,...) {
int i = 0;
fpdata = (float**) malloc((424)*sizeof(float*));
fpdata2 = (float**) malloc((424)*sizeof(float*));
...
for (i = 0; i<424; i++) {
fpdata[i] = (float*) malloc((117)*sizeof(float));
fpdata2[i] = (float*) malloc((117)*sizeof(float));
} // for
} // allmem()
mallocreturn successfully?