M
Marlene Stebbins
Something very strange is going on here. I don't know if it's a C
problem or an implementation problem. The program reads data from
a file and loads it into two arrays. When xy, x, y, *xlist and
*ylist are ints or floats there is no apparent problem. If these
variables are doubles, the program crashes. Furthermore, the
crashes occur only when xlist and ylist are free()ed. When the
above variables are doubles and the calls to free() are
eliminated, the program doesn't crash, but the output is weird.
If there is anyone out there with enough time on his hands to
compile this code and play with it, I would appreciate getting
your opinion. Don't forget to change the format specifiers in the
calls to fscanf() when changing types. Here are data for the
input file:
0 12 5 12 8 9 11 3 10 -2 5 -8 0 -10 -7 -1 -7 12
/* Read xy data from a file.
Load x&y values into respective arrays.
Works with ints, floats, but not doubles???
*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double xy, x, y;
double *xlist, *ylist;
int xct, yct, xycount, idx;
FILE *data;
if((data = fopen("vertices.txt", "r")) == NULL)
{
fprintf(stderr, "can't open data file\n");
exit(EXIT_FAILURE);
}
xycount = 0;
while(fscanf(data, "%lf") == 1)
xycount++;
if(xycount%2 != 0)
{
fprintf(stderr, "unequal number of x,y values\n");
exit(EXIT_FAILURE);
}
xlist = calloc(xycount/2, sizeof(xlist));
ylist = calloc(xycount/2, sizeof(ylist));
rewind(data);
xct = 0;
yct = 0;
while(fscanf(data, "%lf %lf", &x, &y) == 2)
{
xlist[xct++] = x;
ylist[yct++] = y;
}
fclose(data);
for(idx = 0; idx < xycount/2; ++idx)
printf("%4.f", xlist[idx]);
putchar('\n');
for(idx = 0; idx < xycount/2; ++idx)
printf("%4.f", ylist[idx]);
putchar('\n');
free(xlist);
free(ylist);
return 0;
}
problem or an implementation problem. The program reads data from
a file and loads it into two arrays. When xy, x, y, *xlist and
*ylist are ints or floats there is no apparent problem. If these
variables are doubles, the program crashes. Furthermore, the
crashes occur only when xlist and ylist are free()ed. When the
above variables are doubles and the calls to free() are
eliminated, the program doesn't crash, but the output is weird.
If there is anyone out there with enough time on his hands to
compile this code and play with it, I would appreciate getting
your opinion. Don't forget to change the format specifiers in the
calls to fscanf() when changing types. Here are data for the
input file:
0 12 5 12 8 9 11 3 10 -2 5 -8 0 -10 -7 -1 -7 12
/* Read xy data from a file.
Load x&y values into respective arrays.
Works with ints, floats, but not doubles???
*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double xy, x, y;
double *xlist, *ylist;
int xct, yct, xycount, idx;
FILE *data;
if((data = fopen("vertices.txt", "r")) == NULL)
{
fprintf(stderr, "can't open data file\n");
exit(EXIT_FAILURE);
}
xycount = 0;
while(fscanf(data, "%lf") == 1)
xycount++;
if(xycount%2 != 0)
{
fprintf(stderr, "unequal number of x,y values\n");
exit(EXIT_FAILURE);
}
xlist = calloc(xycount/2, sizeof(xlist));
ylist = calloc(xycount/2, sizeof(ylist));
rewind(data);
xct = 0;
yct = 0;
while(fscanf(data, "%lf %lf", &x, &y) == 2)
{
xlist[xct++] = x;
ylist[yct++] = y;
}
fclose(data);
for(idx = 0; idx < xycount/2; ++idx)
printf("%4.f", xlist[idx]);
putchar('\n');
for(idx = 0; idx < xycount/2; ++idx)
printf("%4.f", ylist[idx]);
putchar('\n');
free(xlist);
free(ylist);
return 0;
}