Workarounder
Programmer
Hi!
I have a program and in this program I have to do a lot of close´s and free´s. The program is something like this:
int main(int argc, char** argv) {
FILE* pFile = NULL;
FILE* pFile2 = NULL;
char* pChar = NULL;
pChar = (char*)malloc(30);
/* and more lines of code... */
return finalizing(pFile, pFile2, pChar);
}
static void finalize(FILE* f1, FILE* f2, char* c) {
if (f1 != NULL) {
fclose(f1);
}
if (f2 != NULL) {
fclose(f2);
}
if (f1 != NULL) {
free(c);
}
return 0;
}
My question is: will I have problems if I use free() function in the finalize() function? I mean, do we have to use free() only in the same function where we use malloc()? In other words, I am using malloc in the main() function, but I am calling free() in the finalize(), so the memory will not be deallocated. Does that make sense?
Thanks.
I have a program and in this program I have to do a lot of close´s and free´s. The program is something like this:
int main(int argc, char** argv) {
FILE* pFile = NULL;
FILE* pFile2 = NULL;
char* pChar = NULL;
pChar = (char*)malloc(30);
/* and more lines of code... */
return finalizing(pFile, pFile2, pChar);
}
static void finalize(FILE* f1, FILE* f2, char* c) {
if (f1 != NULL) {
fclose(f1);
}
if (f2 != NULL) {
fclose(f2);
}
if (f1 != NULL) {
free(c);
}
return 0;
}
My question is: will I have problems if I use free() function in the finalize() function? I mean, do we have to use free() only in the same function where we use malloc()? In other words, I am using malloc in the main() function, but I am calling free() in the finalize(), so the memory will not be deallocated. Does that make sense?
Thanks.