I often use the website www.cplusplus.com as a reference when writing the code.
I was quoted on the page to read the example and a question
was an example as a post.
/ * fread example: reading a complete file * / #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; Int main () {FILE * pFile; Longer size; Four * buffer; Size_t result; PFile = fopen ("myfile.bin", "RB"); If (pFile == NULL) {fputs ("file error", stderr); Exit (1);} // Get the file size: fseek (pFile, 0, SEEK_END); LSize = ftell (pFile); Rewind (Pfile); // Allocate memory to keep the full file: buffer = (four *) mauloq (sizeoff (lèse) * lSize); If (buffer == faucet) {fputs ("memory error", stderr); Exit (2);} // Copy the file into buffer: result = Fred (buffer, 1, lcase, pfile); If (result! = LSize) {fputs ("Reading error", stderr); Exit (3);} / * The whole file is loaded into memory buffer * / // closing fclose (pFile); Free (buffer); Return 0; }
I think if the result! = Lceize, then free (buffer) will never be called. Can this be a memory leak in this example?
I have always thought that the examples present on their site are very high quality, maybe I can not understand correctly?
This will not be a memory leak in this example , because the program ends Frees all the memory associated with (by calling exit ()
).
However, if you use this piece, then this memory leak will be exit ()
.
Comments
Post a Comment