So, this is a pretty simple question I'm assuming, but I am new to memory management in general so I was wondering if you guys could help me.
I was wondering if this code has a memory leak?: [C] #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> - Pastebin.com
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char **argv) {
FILE *file_handle = fopen("in.xx", "rb");
int c;
char *delta = malloc(1);
delta[0] = '\0';
int delta_length = 0;
do {
c = fgetc(file_handle);
if (isspace(c) || c == EOF) {
if (delta_length == 0) {
goto free_delta;
}
printf("%s\n", delta);
free_delta:
free(delta);
delta = malloc(1);
delta[0] = '\0';
delta_length = 0;
} else {
delta = realloc(delta, 1);
delta_length += 1;
delta[delta_length - 1] = c;
delta[delta_length] = '\0';
}
} while (c != EOF);
free(delta);
fclose(file_handle);
return 0;
}
I've ran it through "Deleaker" on Visual Studio and it says it does but it looks alright to me...
Also, if anyone knows of a good resource of the do's and don'ts of memory management I'd love to hear it.
Thanks.