Re: strange behavior
From: Darrell Grainger (darrell_at_NOMORESPAMcs.utoronto.ca.com)
Date: 04/23/04
- Next message: Booted Cat: "Named argument view for IDE and more"
- Previous message: Kenneth Brody: "Re: Valid operations on pointers in C"
- In reply to: Marlene Stebbins: "strange behavior"
- Next in thread: Martin Ambuhl: "Re: strange behavior"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 23 Apr 2004 20:23:36 GMT
On Fri, 23 Apr 2004, Marlene Stebbins wrote:
> 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++;
Where are you storing the data that fscanf is reading it? This could cause
a program to crash. I'd use:
while(fscanf(data, "%lf", &xy) == 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));
Obviously the /2 is okay. You want to store half the list in xlist and the
other half in ylist. The problem here is that sizeof(xlist) is the size of
a pointer to double. You want the size of a double or sizeof(*xlist). Same
thing for 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');
Just as a side note, a good compiler will do this for you but I'm in the
habit of taking things like xycount/2 and replacing them with another
variable. In a for loop, like above, this is a chance division will occur
on each iteration of the loop. That could have a notble impact.
> free(xlist);
> free(ylist);
>
> return 0;
> }
>
>
>
>
-- Send e-mail to: darrell at cs dot toronto dot edu Don't send e-mail to vice.president@whitehouse.gov
- Next message: Booted Cat: "Named argument view for IDE and more"
- Previous message: Kenneth Brody: "Re: Valid operations on pointers in C"
- In reply to: Marlene Stebbins: "strange behavior"
- Next in thread: Martin Ambuhl: "Re: strange behavior"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|