get size of malloc'd object

Frank Adams franka at mmintl.UUCP
Tue Jul 1 09:14:41 AEST 1986


In article <2219 at peora.UUCP> jer at peora.UUCP writes:
>> Yes, but I think there is a question whether you want to tell the user
>> about 1. how much he ASKED for, or 2. how much happens to BE there...
>
>The only portable value is how much he asked for.  If he got more than
>that, it is just an artifact of the memory allocation scheme, which would
>be implementation-dependent, and thus he shouldn't use it; allowing him to
>know how much extra he really got is a step in the direction of promoting
>him to use it, which would seem not to be a good idea.

Depends on what you mean by "portable".  Certainly, it is possible to use
this number in an implementation-dependent way; but there are natural and
non-implementation-dependent ways to use the information.  E.g., if one is
allocating character strings and one of them grows, one can try to fit the
expanded string into the allocated size, and only reallocate (and reset all
the pointers to the data) if it won't fit.

The philosphy of C is to give the programmer access to information if there
are reasonable ways to use it, not to try to withhold the information if
there are unreasonable ways to use it.

Frank Adams                           ihnp4!philabs!pwa-b!mmintl!franka
Multimate International    52 Oakland Ave North    E. Hartford, CT 06108



More information about the Comp.lang.c mailing list