C Programming: calling free() on error?
Posted
by kouei
on Stack Overflow
See other posts from Stack Overflow
or by kouei
Published on 2010-03-20T14:04:35Z
Indexed on
2010/03/20
14:11 UTC
Read the original article
Hit count: 242
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.
© Stack Overflow or respective owner