Thread: free() doing weird things with file i/o

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    31

    free() doing weird things with file i/o

    So I was trying to practice C as opposed to C++ which I'm more or less used to (at least when it comes to doing hw assignments), and I wrote the following code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       FILE* inFile = fopen("in", "r");
       FILE* outFile = fopen("out", "w");
       while(!feof(inFile))
       {
          char* blah = malloc(sizeof(char));
          fread(blah, sizeof(char), 1, inFile);
          fwrite(blah, sizeof(char), 1, outFile);
          free(blah);
       }
       fclose(inFile);
       fclose(outFile);
       return 0;
    }
    Now obviously my first problem was using feof() incorrectly, but before finding that out I noticed something weird. although I call free() AFTER I call fwrite(), commenting out the free() line changes the extra character in the file. If I have free(), the byte is 00. If I don't, though, the byte is the character for 'x' . Does anyone know why this would be? I would think that code executed after writing to the file would have no effect.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Does anyone know why this would be? I would think that code executed after writing to the file would have no effect.
    Have you read one of many FAQs?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by SgtMuffles
    So I was trying to practice C as opposed to C++ which I'm more or less used to (at least when it comes to doing hw assignments), and I wrote the following code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       FILE* inFile = fopen("in", "r");
       FILE* outFile = fopen("out", "w");
       while(!feof(inFile))
       {
          char* blah = malloc(sizeof(char));
          fread(blah, sizeof(char), 1, inFile);
          fwrite(blah, sizeof(char), 1, outFile);
          free(blah);
       }
       fclose(inFile);
       fclose(outFile);
       return 0;
    }
    Now obviously my first problem was using feof() incorrectly, but before finding that out I noticed something weird. although I call free() AFTER I call fwrite(), commenting out the free() line changes the extra character in the file. If I have free(), the byte is 00. If I don't, though, the byte is the character for 'x' . Does anyone know why this would be? I would think that code executed after writing to the file would have no effect.
    Why do you need malloc? Use static var.

    the difference in the output occured due to the location of the blah memory. When the last fread failed you write to the file some garbage from the memory pointed by the blah pointer. Obviously it points different memory locations when you free the previously allocated memory, or just leave it allocated generating memory leaks
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-13-2007, 12:48 AM