c - Does a memory leak exist? -


i'm looking @ assignment question. there piece of c code:

char* read_from_file (const char* filename, size_t length) {     return null; }  void main (int argc, char **argv) {     char* buff;     if ((buff = read_from_file("test1.txt", 10)) == null) {        fprintf(stdout, "failed read test1.txt\n");     } else {        fprintf(stdout, "buff is: %s\n", buff);     }     if ((buff = read_from_file("test2.txt", 10)) == null) {        fprintf(stdout, "failed read test2.txt\n");     } else {        fprintf(stdout, "buff is: %s\n", buff);     } } 

the question says memory leaking. tells me insert free(buff) statements in main program suppress memory leaking. thought memory leaks associated dynamic memory allocation. well, shouldn't free() used free pointer memory block allocated malloc(), etc.

is there i'm missing assignment question?

note: i'm looking advice , insights.

there missing here. there no memory allocations made in program -- standard library function calls fprintf, doesn't allocate memory (outside of internal bookkeeping isn't visible caller). such, can't leak memory.

is possible you're supposed working different definition of read_from_file function? simple implementation along lines of:

char* read_from_file (const char* filename, size_t length) {     char *buff = malloc(length);     open file, read data buff, close     return buff; } 

would cause program leak memory when used in way shown in main.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -