Re: Newbie question about malloc
From: Chris Torek (nospam_at_torek.net)
Date: 12/17/03
- Next message: Christopher Benson-Manica: "Re: Newbie question about malloc"
- Previous message: Richard Heathfield: "Re: Optmization"
- In reply to: George: "Newbie question about malloc"
- Next in thread: Christopher Benson-Manica: "Re: Newbie question about malloc"
- Reply: Christopher Benson-Manica: "Re: Newbie question about malloc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Christopher Benson-Manica: "Re: Newbie question about malloc"
- Previous message: Richard Heathfield: "Re: Optmization"
- In reply to: George: "Newbie question about malloc"
- Next in thread: Christopher Benson-Manica: "Re: Newbie question about malloc"
- Reply: Christopher Benson-Manica: "Re: Newbie question about malloc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|