C Programming: calling free() on error?
- by kouei
Hi all,
This a follow up on my previous question. link here.
My question is: Let's say I have the following code..
char* buf = (char*) malloc(1024);
...
for(; i<20; i++) { 
    if(read(fd, buf, 1024)   == -1) { // read off a file and store in buffer
         perror("read failed");
         return 1;
    }
    ...
}
free(buf);
what i'm trying to get at is that - what if an error occurs at read()? does that mean my allocated memory never gets freed? If that's the case, how do I handle this? Should I be calling free() as part of error handling?
Once again, I apologize for the bad English. ^^;
Many thanks, K.