J
John L
Hello!
I'm somewhat new to C, and the problem is I'm not sure how to structure my
programs. I keep ending up with massively long main() functions that look
like this:
int main(void)
{
declarations;
if (file_didnt_open_or_something) {
return EXIT_FAILURE;
}
if (mem_didnt_allocate_or_something) {
close file;
return EXIT_FAILURE;
}
if (another_malloc_failed_or_something) {
free mem1;
close file;
return EXIT_FAILURE;
}
if (fread_failed_or_something) {
free mem2;
free mem1;
close file;
return EXIT_FAILURE;
}
/* etc. */
free(mem2)
free(mem1);
fclose(file);
return 0;
}
I'm pretty sure I'm not using C's control flow constructions intelligently
at all, since this reminds me of x86 assembly language! How would you guys
arrange a program like the one here?
Advice much appreciated
John
I'm somewhat new to C, and the problem is I'm not sure how to structure my
programs. I keep ending up with massively long main() functions that look
like this:
int main(void)
{
declarations;
if (file_didnt_open_or_something) {
return EXIT_FAILURE;
}
if (mem_didnt_allocate_or_something) {
close file;
return EXIT_FAILURE;
}
if (another_malloc_failed_or_something) {
free mem1;
close file;
return EXIT_FAILURE;
}
if (fread_failed_or_something) {
free mem2;
free mem1;
close file;
return EXIT_FAILURE;
}
/* etc. */
free(mem2)
free(mem1);
fclose(file);
return 0;
}
I'm pretty sure I'm not using C's control flow constructions intelligently
at all, since this reminds me of x86 assembly language! How would you guys
arrange a program like the one here?
Advice much appreciated
John