Need advice...code structure for exit from error in c ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Need advice...code structure for exit from error in c ?
just need a little bit advice for code flow design from experienced c/c++ programmer here...
If I want to terminate a program from error (let's say it's not a too complex program),
should I terminate the program from an error handling function(with possibly in different source file from
the function that invokes error handling function) OR go to error handling and then back to the current function and exit
program from the function itself ?
One consideration is whether functions have to do any cleanup before exit such as close files and/or release dynamic memory. If so then you want to exit back up through the function chain in the same order that you came down through the functions. Or you need some sort of error cleanup routine.
Why does one need to perform any file close or memory cleanups upon exit? This is done BY exit.
This is true when you are running an application program which exits to an operating system. It may or may not be true when you are running a transaction which exits to middleware.
I suppose "terminate the program" is satisfying enough for me that the OP is talking about a basic, good ol' fashion POSIX or similar binary that exits to the OS via _exit(). Typically such a question like this is from a new programmer, learning the basics, and not programming some fringe environment.
But you're right, there can be all sorts of variants.
The kernel returns free pages back to the pool upon process exit.
The _exit() call closes all file descriptors
All memory is freed and returned to the free pool on exit.
This is all part of the C runtime, and standard kernel process management.
[ Note: in general, I'm in complete agreement with your belief in good programming practices and demands of your empolyees ]
What is nonsense is the single-minded policy. We need to teach when/where/how to think and learn, not to mindlessly follow dogmatic rule, all the while not having a clue as to what is going on.
You do rely on the OS to do its job - or it would come to a screeching halt fairly quickly.
I doubt you close FD 0-2 before you call exit. In fact, you'd have to check first to see if they were even open! You don't reset your brk. You probably don't call flush before a close. These are things you must rely on from the system at hand.
There is no need to free malloc'd space in essentially linear, simple programs where the C runtime guarantees cleanup. How do you do garbage collection in a language like Perl? You can destroy, but beyond that, its op to the interpreter and OS to *do their job*. I'm sure you don't do SV cleanup. In kernel development, or microcode, you are concerned about every byte. In higher level languages, we let the OS and runtime environment take care of many things.
It is not about laziness. It is about knowing when and how the tools, environment, and systems work, and what is ***guaranteed*** and what is not.
Adding superfluous code increases risk, increases code coverage requirements, increases complexity. The pitfalls of an errant free() are problematic.
I'm fully aware of the requirements to manage resources and cleanup when and where necessary; I'm also fully aware of when and where it is unnecessary. I feel this is more important to teach than mindless, one-size-fits-all rules. We need to teach why, not do as I say or I'll fire you. Knowledge impresses, threats are for children.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.