Re: Newbie question about malloc

From: Chris Torek (nospam_at_torek.net)
Date: 12/17/03


Date: 17 Dec 2003 20:27:02 GMT

In article <news:810cca80.0312170546.78c084a3@posting.google.com>
George <gkarm@softhome.net> writes:
>Should'nt i get always the number of bytes
>allocated by "malloc" ??? (in this example 16)

In addition to the other responses so far, I might point out that
while the *call* says:

    malloc(16)

there is no guarantee that you actually *got* 16 bytes, even if
the returned pointer is NULL. Suppose, for instance, that for some
reason your implementation has chosen never to hand out less than
some minimum number of bytes at a time via malloc(). The malloc()
on that system might include code like:

    if (size < MIN_MALLOC_SIZE)
        size = MIN_MALLOC_SIZE;

near the top, with MIN_MALLOC_SIZE greater than 16. Thus, you
might have gotten 20, 24, 32, or even more bytes. (Real malloc()s
often do contain this kind of code, so this is not a purely
theoretical issue.)

C has no "how much did I malloc() with this non-NULL pointer that
is suitable for use as an argument to free()" function. Such a
function might be useful, but if one were to be proposed for addition
to a future C standard (or any other "non-C" standard that acts as
a sort of add-on, such as the POSIX system-function bindings like
readdir() and such), we would have to decide: "Does this function
return the argument passed to malloc(), or does it return the actual
number of bytes malloc() handed out even if that was a larger
number?"

Also, this:

> char* name;
    ...
> printf("size=%d \n",sizeof(name));

printed 4. (Note that strlen() returns a size_t value, which is not
an int value; anything might have been printed, but for luck. Whether
this is "good luck" or "bad luck" depends on one's point of view...)
In a later followup he asked:

>... is this a choice of the compiler or the implementation?

The only correct answer to this is "yes", because it might be either
one. :-) (I assume here "implementation" really means "hardware
for which the C compiler compiles machine-level code.") Computer
hardware often suggests (with varying degrees of strength) particular
implementations for C code, but C compilers are not required to
produce "sensible" code, merely "code that obeys the requirements
of the C standard". As such, a compiler for a machine with 32-bit
pointers could generate 16-bit-pointer code (that can only address
65536 bytes at a time) and "run" that code via an interpreter that
simulates some other machine that only has 16-bit pointers.

(This, too, is not purely theoretical -- it is how emulators work.
One might either use a cross-compiler that runs native and produces
code for the emulated hardware, or even run under the emulator an
old "native" C compiler that works on the emulated machine. In
the latter case one might reasonably claim that the "machine" really
does have 16-bit pointers; the cross-compiler case is more
interesting, particularly if it is the cross-compiler that emits
the emulator, and can optionally run some code "native" intermixed
with other code "emulated".)

-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: forget about it   http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


Relevant Pages

  • Re: Array of pointers in a struct
    ... >> This is an array of 27 pointers. ... >> the compiler from warning you that you forgot to include stdlib.h. ... >cast it, there's a warning, if I don't, there isn't. ... warning tells you the compiler thinks malloc is returning an integer. ...
    (comp.lang.c)
  • Re: Available Program memory
    ... valid pointers returned from malloc that you /do/ want the emulator to ... emulated system's memory to only be able to access the emulated ... Just ensure that you emulated pointers are kept in range for the ... For instance, a Z80 emulator with 64K of memory at R10, accessed ...
    (comp.sys.acorn.programmer)
  • Re: Java References
    ... It makes it possible to write malloc in C. ... A C pointer is a memory address. ... Generally speaking, compiler doesn't ... makes perfect sense that all pointers are void.Adding types to ...
    (comp.lang.java.programmer)
  • Re: Java References
    ... It makes it possible to write malloc in C. ... A C pointer is a memory address. ... Generally speaking, compiler doesn't ... makes perfect sense that all pointers are void.Adding types to ...
    (comp.lang.java.programmer)
  • Re: new IL: C (sort of...).
    ... only for "recent" Pascals, ... far pointers weren't really limited, ... in my compiler, I made wchar_t a builtin type (in most cases, aliased to ... I could very well include builtin "managed strings" in the new IL. ...
    (comp.lang.misc)