P
partremmaps
According to the ANSI C Standard, may free() be an empty function?
I am not talking about implementations which do not support dynamic memory allocation.
I am also not talking about cases where free() is passed as an argument something other than a current valid pointer earlier returned by a successful call to calloc, malloc, or realloc.
I am talking about implementations and architecture which fully support malloc, calloc, and realloc, and the situation where free() has been passed a valid and current pointer to currently allocated space, having been allocated earlier by a successful call by malloc().
A couple of people who are in the daily habit of giving C advice on IRC told me that even in these implementations and situations, free() may be an empty function and do nothing and still be standards compliant.
Their reasoning is thus:
"Because the C program cannot, using only semantics of the C Abstract Machine, determine that free() did nothing, it is therefore compliant for free()to do nothing even though it was passed a pointer to valid allocated spacehaving earlier been allocated by malloc()."
and
"On a system where malloc and free are fully supported by hardware and software, free() still may be void and empty and always do nothing while still being ANSI C compliant because free() never has a measurable effect, so nothing need be done to produce a measurable effect."
I protested that if a program were to allocate and free memory repeatedly, and that if free() were allowed to perform no action, that the computer would run out of memory even though it actually had plenty of memory for the program if free() did as described.
They responded that it was up to the user to select an implementation that did what he needed.
The standard of course does describe free as causing the space pointed by ptr to be deallocated and made available for further allocation.
My concern is not that somebody may try to run a program on a machine whichdoes not have enough memory, but that the program will end up needing an infinite quantity of memory if it is run for long enough, even though it waswritten in such a way as to free the memory it allocated before allocatingmore.
If these fellows' claims are true, then this raises more questions like whether input and output functions must also do their described task even under circumstances where the C program, using only semantics of the C AbstractMachine, has no way to determine that the described side effect actually took place.
For example, if stat and fopen and all file open/existence related functions were "optimized" in such a way to always falsely return "file not found,"then the C program, when using only the semantics of the C Abstract machine, would not be able to tell that the file did actually exist, and thus theimplementation should be considered compliant.
For another example, what if putchar() was "optimized" to simply return thecharacter supplied to it as an argument but neglected to actually put the char to stdout - the C program could not test whether the char was actuallysent to stdout.
These fellows responded that a human could observe that a file did indeed exist or that nothing was being sent to stdout, and that was sufficient to observe a missing side effect.
However, they said that a human observing a memory leak was not allowed as a test but that the test of whether a side effect was performed could only be tested by the C program in question, and only within the semantics of the C Abstract machine.
Furthermore, If said claim is true, then it raises the question of why doesthe standard describe a specific functionality if it doesn't really matterif free() does anything anyway?
To me, memory is a finite resource, and whether free frees it as described does matter, which is why the standard specifies an action for free.
In other words, for free() to fail to do as the standard describes, the operation of the program could be severely affected.
They cite the standard where it talks about optimization and says "An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced"
(See section "Program Execution" in the standard.)
The value of free() may not be used, but the freed memory well could be used.
The standard also says that modifying an object (or calling a function thatdoes so) is a side effect.
(See section "Program Execution".)
But does free() particularly modify an object? It does delete it, but is that considered a modification to the execution environment?
But I guess the question is whether one ought to take a subtle technical wording in such a way as to render other explicit content of the standard to be meaningless in such a way that the program would behave drastically outside of how it was written according to the standard.
The standard does say (in cases where there is an operating system other than the C program in question) that free() shall conform to the described specification if it is present. (See "Execution environments" and "Hosted environment.")
To me, common sense says that it would be incorrect to construe a vague technical wording to render explicit portions of the standard meaningless.
My view is that, according to the standard, free() must do exactly as described in the standards under the section "The free function," and that it makes no sense to construe the standard in such a way as to void its very explicitly described functionality of various functions.
Is there a definitive answer on whether free() may be empty and void and still standards compliant, in the context of this post?
Thanks,
~Jesse
I am not talking about implementations which do not support dynamic memory allocation.
I am also not talking about cases where free() is passed as an argument something other than a current valid pointer earlier returned by a successful call to calloc, malloc, or realloc.
I am talking about implementations and architecture which fully support malloc, calloc, and realloc, and the situation where free() has been passed a valid and current pointer to currently allocated space, having been allocated earlier by a successful call by malloc().
A couple of people who are in the daily habit of giving C advice on IRC told me that even in these implementations and situations, free() may be an empty function and do nothing and still be standards compliant.
Their reasoning is thus:
"Because the C program cannot, using only semantics of the C Abstract Machine, determine that free() did nothing, it is therefore compliant for free()to do nothing even though it was passed a pointer to valid allocated spacehaving earlier been allocated by malloc()."
and
"On a system where malloc and free are fully supported by hardware and software, free() still may be void and empty and always do nothing while still being ANSI C compliant because free() never has a measurable effect, so nothing need be done to produce a measurable effect."
I protested that if a program were to allocate and free memory repeatedly, and that if free() were allowed to perform no action, that the computer would run out of memory even though it actually had plenty of memory for the program if free() did as described.
They responded that it was up to the user to select an implementation that did what he needed.
The standard of course does describe free as causing the space pointed by ptr to be deallocated and made available for further allocation.
My concern is not that somebody may try to run a program on a machine whichdoes not have enough memory, but that the program will end up needing an infinite quantity of memory if it is run for long enough, even though it waswritten in such a way as to free the memory it allocated before allocatingmore.
If these fellows' claims are true, then this raises more questions like whether input and output functions must also do their described task even under circumstances where the C program, using only semantics of the C AbstractMachine, has no way to determine that the described side effect actually took place.
For example, if stat and fopen and all file open/existence related functions were "optimized" in such a way to always falsely return "file not found,"then the C program, when using only the semantics of the C Abstract machine, would not be able to tell that the file did actually exist, and thus theimplementation should be considered compliant.
For another example, what if putchar() was "optimized" to simply return thecharacter supplied to it as an argument but neglected to actually put the char to stdout - the C program could not test whether the char was actuallysent to stdout.
These fellows responded that a human could observe that a file did indeed exist or that nothing was being sent to stdout, and that was sufficient to observe a missing side effect.
However, they said that a human observing a memory leak was not allowed as a test but that the test of whether a side effect was performed could only be tested by the C program in question, and only within the semantics of the C Abstract machine.
Furthermore, If said claim is true, then it raises the question of why doesthe standard describe a specific functionality if it doesn't really matterif free() does anything anyway?
To me, memory is a finite resource, and whether free frees it as described does matter, which is why the standard specifies an action for free.
In other words, for free() to fail to do as the standard describes, the operation of the program could be severely affected.
They cite the standard where it talks about optimization and says "An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced"
(See section "Program Execution" in the standard.)
The value of free() may not be used, but the freed memory well could be used.
The standard also says that modifying an object (or calling a function thatdoes so) is a side effect.
(See section "Program Execution".)
But does free() particularly modify an object? It does delete it, but is that considered a modification to the execution environment?
But I guess the question is whether one ought to take a subtle technical wording in such a way as to render other explicit content of the standard to be meaningless in such a way that the program would behave drastically outside of how it was written according to the standard.
The standard does say (in cases where there is an operating system other than the C program in question) that free() shall conform to the described specification if it is present. (See "Execution environments" and "Hosted environment.")
To me, common sense says that it would be incorrect to construe a vague technical wording to render explicit portions of the standard meaningless.
My view is that, according to the standard, free() must do exactly as described in the standards under the section "The free function," and that it makes no sense to construe the standard in such a way as to void its very explicitly described functionality of various functions.
Is there a definitive answer on whether free() may be empty and void and still standards compliant, in the context of this post?
Thanks,
~Jesse