Hi there,
this is my first post as I like to try and work things out wherever possible without bothering other people, but I'm completely at a wit's end with this one.
I am trying to read in a 2D array of unknown length from a file into "myarray" using malloc and realloc as I saw this method recommended elsewhere. The data to be read in is known to be 4 columns but the number of rows is not known.
So far I manage to allocate 5000 pointers (I think), each pointing to 4 doubles.
Code:
int datasize=0;
int nrows=5000;
float** myarray=(double**)malloc(nrows*sizeof(double*));
for(i=0;i<=nrows;i++)
{
myarray[i]=(double*)malloc(4*sizeof(double));
}
When this runs out, I attempt to add another 1000 rows with realloc.
Code:
if(datasize==nrows)
{
nrows+=1000;
myarray=(double**)realloc(myarray,nrows*sizeof(double*));
for(j=0;j<nrows;j++)
{
myarray[j]=(double*)realloc(myarray[j],4*sizeof(double));
printf("%d\n",j); //this is just to see how far through the programme it gets
}
}
When I run this, I get the pop-up "Data Fitting.exe has stopped working" at one of three points. Either at the first attempt of memory allocation i.e. j=0, or when j=5074 or j=5110. There is never any variation from this. Running a debugger shows there is a segmentation fault (SIGSEGV). I understand this has something to do with memory but I do not really know exactly what it means.
I am new to C so sorry if the answer is very simple. I would be extremely grateful for any insight as I have spent a long long time on this.
Here is my whole code for reference.
Code:
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////dynamic version////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
FILE *myfile;
int i=0;
int j=0;
int c=0;
int datasize=0;
int nrows=5000;
int elements=0;
float x,y,sx,sy;
double a, b, SigA, SigB;
char fname[100]={0};
double** myarray=(double**)malloc(nrows*sizeof(double*));
for(i=0;i<=nrows;i++)
{
myarray[i]=(double*)malloc(4*sizeof(double));
}
//1) Ask user to input filename from keyboard
printf("Enter a file path:\n");
scanf("%s",fname);
if (fname[0]=='\"') //removes quotations automatically inserted when copying a file path from windows explorer
{
for ( c = 0 ; c < 99 ; c++ )
{
fname[c] = fname[c+1];
}
for ( c = 0 ; c < 100 ; c++ )
{
if (fname[c]=='\"')
{
fname[c]=0;
}
}
}
//2) Call function to read in data from file (check the file was actually found and read in correctly)
myfile=fopen(fname,"r");
if (myfile!=NULL)
{
for (i=0; !feof(myfile); i++)
{
fscanf(myfile,"%f %f %f %f",&x,&y,&sx,&sy);
myarray[i][0]=x;
myarray[i][1]=y;
myarray[i][2]=sx;
myarray[i][3]=sy;
datasize++;
if(datasize==nrows)
{
nrows+=1000;
myarray=(double**)realloc(myarray,nrows*sizeof(double*));
for(j=0;j<nrows;j++)
{
myarray[j]=(double*)realloc(myarray[j],4*sizeof(double));
printf("%d\n",j); //this is just to see how far through the programme it gets
}
}
}
elements = i-1;
fclose(myfile);
}
else
{
printf("File does not exist");
}
//3) Call the function to calculate the coefficients
double sumyoversqsig=0;
double sumsqxoversqsig=0;
double sumxyoversqsig=0;
double sumoneoversqsig=0;
double sumxoversqsig=0;
for (i=0; i<(elements); i++)
{
sumyoversqsig+=((myarray[i][1])/pow(myarray[i][3],2));
sumsqxoversqsig+=((pow(myarray[i][0],2))/pow(myarray[i][3],2));
sumxoversqsig+= ((myarray[i][0])/pow(myarray[i][3],2));
sumxyoversqsig+=(((myarray[i][0])*(myarray[i][1]))/pow(myarray[i][3],2));
sumoneoversqsig+=(1/pow(myarray[i][3],2));
}
printf("datasize = %d \nnrows = %d",datasize,nrows);
printf("%f\n%f\n%f\n%f\n%f\n",sumyoversqsig, sumsqxoversqsig, sumxoversqsig, sumxyoversqsig, sumoneoversqsig);
a=((sumyoversqsig*sumsqxoversqsig)-(sumxoversqsig*sumxyoversqsig))/((sumoneoversqsig*sumsqxoversqsig)-pow(sumxoversqsig,2));
b=((sumoneoversqsig*sumxyoversqsig)-(sumyoversqsig*sumxoversqsig))/((sumoneoversqsig*sumsqxoversqsig)-pow(sumxoversqsig,2));
SigA=sqrt((sumsqxoversqsig)/((sumoneoversqsig*sumsqxoversqsig)-pow(sumxoversqsig,2)));
SigB=sqrt((sumoneoversqsig)/((sumoneoversqsig*sumsqxoversqsig)-pow(sumxoversqsig,2)));
//4) Display coefficients on the screen. ;
printf("Coefficient a =%le\nCoefficient b =%le\nSigma a =%le\nSigma b =%le\n",a,b,SigA,SigB);
free(myarray);
return 0;
}