Mail Archives: djgpp/1995/04/02/19:15:41
> ASSUMPTION 1. When some function allocates memory, this memory will not
> be freed when the function returns. This implies:
True.
> a/ If the function does not free the memory block and does not return
> the pointer, that memory block will be occupied until the program
> stops, and there is no way to free it.
True.
> b/ If the function returns the pointer, it is possible to use it and
> even to free it.
True.
> ASSUMPTION 2. the Operating System always knows the size of an allocated
False. The malloc() function can figure it out. The OS only knows
how *much* memory the program has.
> block simply by indicating the base address (when I call free() I simply
> gives this argument), no matter if I am in the same funcion that
> allocated the memory block or not.
True.
> If ASSUMPTION 2 is true, is there an ANSI-C manner to get from the O.S.
> the amount of memory a pointer points to? I have tried "sizeof(pointer)",
> but it only returns the size of a single element.
There is no portable way of doing this. ANSI and POSIX specifically
stay away from memory allocation techniques. In theory, you may be
able to reverse-engineer malloc() (or, for djgpp, just read the
sources) and see how free() figures it out, but in general such
functionality is not available. Given the existence of realloc(), it
often does not need to be available.
- Raw text -