I am a little bit puzzled by the behavior of some static variables. Here's the code:
Code:
#include<stdio.h>
#include<stdlib.h>
double** input_file(int *);
void read_file();
int main()
{
int i;
for (i=0;i<4;i++)
read_file();
return 0;
}
void read_file()
{
double **lance_coord;
static int file_was_read=0;
int nlances;
int i;
if (!file_was_read)
{
printf("here\n");
lance_coord=input_file(&nlances);
file_was_read=1;
}
printf("nlances= %10d\n",nlances);
for (i=0;i<nlances;i++)
{
printf("%10f %10f %10f\n",*(*(lance_coord+i)),*(*(lance_coord+i)+1),*(*(lance_coord+i)+2));
}
}
double** input_file(int *nlances)
{
double d[3], **lance_coord;
int i,j;
FILE *fp;
char* fname="/home/dralexpe/bas_screamer/C_learning/C_tests/io.txt";
fp=fopen(fname, "r");
if (fp==NULL) printf("I/O error\n");
*nlances=0;
while (!feof(fp))
{
fscanf(fp,"%lf %lf %lf", d,d+1,d+2);
(*nlances)++;
}
rewind(fp);
lance_coord=(double**)calloc(*nlances,sizeof(double*));
for (i=0;i<*nlances;i++)
{
fscanf(fp,"%lf %lf %lf",d,d+1,d+2);
*(lance_coord+i)=(double*)calloc(3,sizeof(double));
for (j=0;j<3;j++)
{
*(*(lance_coord+i)+j)=*(d+j);
}
}
fclose(fp);
return lance_coord;
}
I am trying to read some coordinates from a file, which is done in function double** input_file(int *nlances). I don't know how many lines of coordinates are there, so I am reading the file and counting lines, then rewind and dynamically allocate an array. After that I am reading it the second time and populate the array **lance_coord, which it's returned. The function read_file tests the static variables; I'd like to read the file only once, then keep the array for subsequent calls. The output looks like this:
Code:
[dralexpe@new-screamer C_tests]$ ./ptr.o
here
nlances= 5
-195.874000 56.123500 89.452000
-8956.123000 4.789650 -456.789451
78.456000 -12035.458970 -45.458890
-10256.780000 45.789000 12345.125670
-5689985.230000 45.200000 10000.265416
nlances= 5
-195.874000 56.123500 89.452000
-8956.123000 4.789650 -456.789451
78.456000 -12035.458970 -45.458890
-10256.780000 45.789000 12345.125670
-5689985.230000 45.200000 10000.265416
nlances= 5
-195.874000 56.123500 89.452000
-8956.123000 4.789650 -456.789451
78.456000 -12035.458970 -45.458890
-10256.780000 45.789000 12345.125670
-5689985.230000 45.200000 10000.265416
nlances= 5
-195.874000 56.123500 89.452000
-8956.123000 4.789650 -456.789451
78.456000 -12035.458970 -45.458890
-10256.780000 45.789000 12345.125670
-5689985.230000 45.200000 10000.265416
In function read_file() the variable nlances is not static, thus it should not retain its value between function calls, yet it does retain it. Why?
Another thing is that the pointer **lance_coord, defined in read_file() is pointing at a memory area on the heap, but the pointer itself is not on the heap. What I am thinking is, because I didn't release the memory on the heap with free(), that memory is not released, and the pointer to it should have been lost, causing a memory leak. Apparently, the pointer **lance_coord is pointing at the same area between subsequent calls of the function read_file(), as shown in the output.
This is just a test program to understand the dynamic memory allocation, I will be using this knowledge for a different application.
Thank you.