>>> fopen() is not a memory allocation function. It is a file opening
>>> function. Its compliment is fclose(). You cannot use free() on it.
>> [...] return value of fopen() is actually the return value of
>> the malloc() that fopen() calls to allocate the space
>> for the FILE struct. Because of this, it can also be free()d.
> 1. No one says that the FILE* returned by fopen needs to be on the heap.
t
> may be a pointer into a static structure.
> 2. No one says that the FILE* returned by fopen needs to be the head of the
> memory block allocated. It could be allocated on the heap as a member of:
> [...]
(Sorry for the awful quote)
Yes, you are absolutely right. I (once again) tried to be smart based on just
a single test I made. Didn't consider that it is actually legal for the C
library to summon nasal demons if free() is called on a FILE * returned by
fopen(). True, no law says that there is one single correct implementation of
fopen() and friends. Mr. McBride and ANSI C standard vs. Mr. Soiha and "quick
thinking": 1-0.
Just that I still think that the most obvious way to implement fopen() and
friends is simply just do a malloc() + relevant magic and return the value of
the malloc()...
--- BBBS/LiI v3.42 ToMmIk-4v
---------------
* Origin: Usagi Tsukino Worship Cult (2:222/222.220)
|