Re: strange behavior

From: Darrell Grainger (darrell_at_NOMORESPAMcs.utoronto.ca.com)
Date: 04/23/04


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


Relevant Pages

  • Re: 32 bit or 64 bit alignment ?! gcc options??
    ... ints and longs will both be 4 bytes and will be 4-byte aligned by ... Doubles will be 4-byte aligned, ... what makes you think that this alignment has anything to do ... or a struct is shared but each single element is not shared. ...
    (comp.programming.threads)
  • Re: Can a double always represent an int exactly?
    ... >given that the details of a type realization varies ... I don't know whether the C Standard specifies anything to ... On a machine with 64-bit doubles which follow the IEEE ... On my machine, which has 32-bit ints and 64-bit doubles, ...
    (comp.lang.c)
  • Re: How to quickly print arrays?
    ... > Is there some little utility function that will let me do ... > where a is an array of doubles, ints, or bytes, ...
    (comp.lang.java.programmer)
  • Re: Natural size: int
    ... A char should be the smallest addressable unit of memory; ... system only supports 16-bit loads, ... You assume that shorter ints are somehow more efficient than longer ...
    (comp.lang.c)
  • strange behavior
    ... *ylist are ints or floats there is no apparent problem. ... variables are doubles, the program crashes. ...
    (comp.lang.c)