-
Notifications
You must be signed in to change notification settings - Fork 273
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
Should we implement standard free
semantics for deallocation functions?
#345
Comments
Yes. It is entirely fine to check for null when deallocating. But your rationale is excellent. |
The main argument against |
@mux If the cost of the branch becomes significant, then I submit to you that we are doing too many allocations. |
Even if that's the case, calling
To the best of my knowledge and googling abilities, this feature is there since the first standard, and it's not a new C99 feature. |
Of course the caller should check for it, that's kind of a circular argument. We are talking about a situation in which there is a bug to begin with. The point is that
I am pretty sure it was first defined that |
My only point is that one should be careful before assuming that something isn't going to matter for everybody, as there is very often a possibility for this judgment to end up being wrong later on. I did say that I agreed it is fairly unlikely to be a problem. In this specific case, it's easy to say someone is doing too many allocations - but you cannot always avoid doing that. Some people work within very specific environments, or have to work with existing code outside of their control that may be wrong in many ways. I don't think it takes a huge imagination to come up with a scenario where this could indeed be an issue, but I don't think there is a point in coming up with a contrived example here, this is more about a philosophy; being aware that something that might seem completely unrealistic to you might in fact happen to somebody someday. |
C89/90:
(emphasis added) |
I have created a PR which, I think, would help solve this issue, please review |
I stand corrected, those discussions I am remembering must have stemmed from something else than the introduction of C99. I stand by my preferences regarding this however, and the reasoning behind why I believe it is better for |
I think it is customary to equate the two in non-formal settings. If I refer to GCC 14, for example, I refer to both the compiler and the libraries that come with it. |
If it's customary then I guess it is a common mistake. The compiler and the standard library are simply two different pieces of software. In Linux systems, glibc is the libc implementation and it has a specific memory allocator implementation (ptmalloc these days, used to be dlmalloc IIRC) and FreeBSD, for instance, has its own libc implementation that uses the jemalloc memory allocator (and used to use phkmalloc a long time ago). I don't know if GCC comes with some malloc implementation, that is not impossible it would have one for very specific circumstances, but it isn't relevant anyways as this is not what would be used when you write a program that uses malloc. |
I think that we understand the StackOverflow answer. From the very beginnng, |
It may very well be the case that you're talking pre-standard, if you worked on it at the time. C is much older than the standardization efforts. I have no idea what K&R C did, for example. I wasn't around (not even born) when C got standardized and only went as far back as the first standard. There's also a chance that ANSI and ISO differ in that. Re: assumptions: at some point you have to make some. Nothing stops us from reacting if and when the situation arises that our assumption is wrong, and then we fix it or work in a workaround. It doesn't make sense IMO to make all users pay proactively by making the interface unforgiving when the standard library is already much friendly, just in case some specific use case can't avoid allocating so much that the branch becomes a problem. |
Lastly, regarding failing early, while I can agree with that philosophy, |
The standard library specifies
free
to act as anop
when its argument is aNULL
pointer. This gives us programmers a big convenience for error handling and similar situations where we don't need to add so many explicit branches, but rather count onfree
doing the right thing for us. Point being, my proposal is not exactly something new.Tackling #182 in particular would create (or, rather, acknowledge) many new error paths that will require these
free
analogs to be called, and the current implementations also mean explicitly checking forNULL
each time on each call place. This can become tedious, error prone and a cognitive load rapidly.It can also help brevity on existing code.
Counterarguments would probably go around the extra branching: sometimes we simply know for certain our pointer won't be
NULL
. IMO this case shouldn't matter that much for performance for the following reasons:free
function is is not the central concern.Thoughts?
The text was updated successfully, but these errors were encountered: