K
Kelsey Bjarnason
[snips]
[cross-post added; CLC can be subsequently removed]
Oh, but it does. It says free releases to be made available for further
allocation. The only things the standard discusses which pertain to
allocation are C functions. Not OS behaviours, not multithreading
schedulers, not network clients and database servers. Nope, it's C
functions, and only C functions, and all done exclusively in the context
of a standalone, isolated, conforming C application set against a
conforming implementation.
In fact, this is a regular topic here in CLC - that "C doesn't do that".
It doesn't do threading. It doesn't do directories. It doesn't do file
systems.
In fact, more generally, it is described in terms of a virtual machine,
whose operation is - as far as is relevant to the code - defined
specifically in terms of operations and definitions from the standard.
This is, for example, how we get the "as if" rule; the implementation
need not work exactly as documented, as long as the results are the same
_as if_ carried out on such a machine.
It's also how we can get things such as fopen to work at all; we define
it in terms _relevant to the code_, not in terms of the OS, as we have no
idea how - or if - the OS handles such things.
In every case, what matters is not the underlying OS or file system or
whether the user is at a keyboard, or a remote terminal, or even isn't a
user at all, but actually a piped-in file providing the input, but
rather, the mechanics of the code as defined by the standard.
One cannot adopt such a stand in the general case then suddenly, in the
case of free, throw up one's hands and suddenly invoke magic operations
of some hypothetical underlying OS.
No, just as we discount the underlying OS when defining how fopen or
getchar works, we must also - to be consistent - discount the underlying
OS when defining how free works. And we do: free releases memory for
subsequent allocation - and the only thing which *can* allocate memory,
the only thing which *exists* as far as the standard is capable of
defining it, is the conforming application, built with a conforming
implementation.
Which means the only thing in the universe which *can* allocate the
memory released by free *is* the application itself. Thus if free
releases the memory back to the OS, it means that free stands unique
among the standard library functions as implicitly assuming operations
beyond the standard, without bothering to document them, not even going
so far as to toss a hat at implementation defined behaviour.
Nope, free documents exactly what it does - releases the memory to be
made available for further allocation - but because of the context in
which that comment is made, the only thing which *can* allocate the
memory is a function or functions applicable to the application - meaning
malloc does qualify, but some hypothetical OS function taking that memory
back doesn't.
[cross-post added; CLC can be subsequently removed]
It does not, if it did, it would explicitly state that and it does not.
Oh, but it does. It says free releases to be made available for further
allocation. The only things the standard discusses which pertain to
allocation are C functions. Not OS behaviours, not multithreading
schedulers, not network clients and database servers. Nope, it's C
functions, and only C functions, and all done exclusively in the context
of a standalone, isolated, conforming C application set against a
conforming implementation.
In fact, this is a regular topic here in CLC - that "C doesn't do that".
It doesn't do threading. It doesn't do directories. It doesn't do file
systems.
In fact, more generally, it is described in terms of a virtual machine,
whose operation is - as far as is relevant to the code - defined
specifically in terms of operations and definitions from the standard.
This is, for example, how we get the "as if" rule; the implementation
need not work exactly as documented, as long as the results are the same
_as if_ carried out on such a machine.
It's also how we can get things such as fopen to work at all; we define
it in terms _relevant to the code_, not in terms of the OS, as we have no
idea how - or if - the OS handles such things.
In every case, what matters is not the underlying OS or file system or
whether the user is at a keyboard, or a remote terminal, or even isn't a
user at all, but actually a piped-in file providing the input, but
rather, the mechanics of the code as defined by the standard.
One cannot adopt such a stand in the general case then suddenly, in the
case of free, throw up one's hands and suddenly invoke magic operations
of some hypothetical underlying OS.
No, just as we discount the underlying OS when defining how fopen or
getchar works, we must also - to be consistent - discount the underlying
OS when defining how free works. And we do: free releases memory for
subsequent allocation - and the only thing which *can* allocate memory,
the only thing which *exists* as far as the standard is capable of
defining it, is the conforming application, built with a conforming
implementation.
Which means the only thing in the universe which *can* allocate the
memory released by free *is* the application itself. Thus if free
releases the memory back to the OS, it means that free stands unique
among the standard library functions as implicitly assuming operations
beyond the standard, without bothering to document them, not even going
so far as to toss a hat at implementation defined behaviour.
Nope, free documents exactly what it does - releases the memory to be
made available for further allocation - but because of the context in
which that comment is made, the only thing which *can* allocate the
memory is a function or functions applicable to the application - meaning
malloc does qualify, but some hypothetical OS function taking that memory
back doesn't.