Reputation: 448
I am trying to write some portable code and I've been thinking how did Microsoft implement old C runtime routines like gmtime or fopen, etc which were returning a pointer, opposite to todays gmtime_s or fopen_s which requires object to passed and are returning some errno status code (I guess).
One way would be to create static (better than global) object inside such routines and return pointer to it, but if one object is currently using this static pointer and another object invokes that routine, first object would get changed buffer - which is not good.
Furthermore, I doubt that such routines uses dynamic memory because that would lead to memory leaks.
As with other Microsoft stuff, implementation is not opened so that I can take a peak. Any suggestions?
Upvotes: 0
Views: 87
Reputation: 214730
I'm not sure about the Visual Studio specifics, but these function libraries are typically implemented as opaque type. Which is why they return a pointer and why you can't know the contents of the FILE
struct.
Meaning there will either be a static memory pool or a call to malloc inside the function. There are no guarantees of the C library functions are re-entrant.
Calling fopen
without having a corresponding fclose
might indeed create a memory leak: at any rate you have a "resource leak". Therefore, make sure that you always call fclose
.
As for the implementation details: you can't have the Visual Studio source code, but you could download
Upvotes: 0
Reputation: 15642
Regarding gmtime
, you are correct; it could have operated upon a variable that has static
storage duration (which is the same storage duration as variables declared "globally", btw... There is no "global" in C). Historically speaking, you should probably assume this is the case, because C doesn't require that there be any support for multithreading. If you're referring to an era where there was decent support for multithreading, it's probable that gmtime
might return something that has thread specific storage duration, instead, as the MSDN documentation for gmtime
says gmtime
and other similar functions "... all use one common tm structure per thread for the conversion."
However, fopen
is a function that creates resources, and as a result it's reasonable to expect that every return value will be unique (unless it's an erroneous return value).
Indeed, fopen
does constitute dynamic management; you are expected to call fclose
to close the FILE
once you're done with it... If you forget to close a file every now and then, there is no need to panic, as the C standard requires that the program close all FILE
s that are still open upon program termination. This implies that the program keeps track of all of your FILE
s behind the scenes.
However, it would obviously be a bad practice to repeatedly leak file descriptors, over and over again, constantly, for a long period of time.
Upvotes: 1
Reputation: 24897
Well, first, such globals and statics cannot be used anyway because of thread-safety.
The use of dynamic memory, or arrays, or arrays of handles, or other such combos DO leak resources if the programmer misuses them. On non-trivial OS, such resources are linked to the process and are released upon process termination, so it is a serious problem for the app, but not for the OS.
Upvotes: 1