-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Safer Dynamic Memory Allocation #2
Comments
Yes, this topic is not addressed in this repository. Clearly, it doesn't cover all types of programming errors. Only those that the compiler can safely warn us against. Sane programming practices, which are very helpful to mitigate presence and improve detection of programming errors, do not necessarily leverage the compiler, and are more dependent on the programmer and reviewer to properly follow a method or pattern. I believe this is what you present in your essay. I believe it's a reasonably good pattern. If followed strictly, it should lead to better control over risks of memory leaks. There are 2 comments that come to mind :
This second question is probably the more important one. For situations that definitely happen when the pattern is violated, such as memory leak, I recommend to run standard tests under a memory sanitizer or a valgrind VM. They should both quickly report a leak issue if there is one. |
The answer is “Nassi-Shneiderman all the way”. That means no early returns. The thing with this pattern is, violations should stand out like a sore thumb and be easy to spot. |
Your essay covers some interesting points, but I notice it skirts the issue of managing dynamic objects. There are times when you have no choice about this. For example, I deal a lot with shareable libraries that offer their services through opaque objects. Since different versions of the libraries might have entirely different sizes and internal layouts of these objects, it is not practicable to let the caller allocate them on the stack; they have to go on the heap.
And then you have the well-known complications of checking for errors on such allocations, and ensuring that everything is correctly cleaned up on both success and failure code paths. All too often I see a rat’s nest of
goto
s to deal with this, when really the simplest solution is to avoidgoto
s altogether.More explanation (with examples) here: https://github.com/ldo/a_structured_discipline_of_programming
The text was updated successfully, but these errors were encountered: