Re: malloc and realloc



On Jan 27, 9:16 pm, "Malcolm McLean" <regniz...@xxxxxxxxxxxxxx> wrote:
"Richard Heathfield" <r...@xxxxxxxxxxxxxxx> wrote in message
The absolute limit for any one call is (size_t)-1 bytes - i.e. the largest
value that can be stored in a size_t, because that's the largest value you
can pass to malloc. But you should not be surprised if implementations do
not allow you to allocate this much. ALWAYS check the result of malloc to
determine whether the call succeeded before trying to use the space you
requested.
Actually Windows guarantees that allocations of under 8K or something like
that will succeed, unless the program is responding to a request to shut
down due to lack of memory, in which case 64K of allocation or so is
guaranteed, but not an infinite supply of 64K chunks.
I'll have to ask for chapter&verse on this one. If you're referring to
Windows malloc(), the manual explicitly states "Always check the
return from malloc, even if the amount of memory requested is small".
Furthermore, malloc() uses HeapAlloc() to get the memory: "Memory
allocated by HeapAlloc is not movable. Because the memory is not
movable, the heap can become fragmented". In theory, it could become
so fragmented, that you wouldn't be able to get 8K.
So on a modern operating system you don't need to check small mallocs. You
do if code is to be completley portable, of course.
The conclusion is drawn from Windows, can you be sure this should
apply to all other systems as well?
--
WYCIWYG - what you C is what you get

.



Relevant Pages

  • Re: Review: My C FAQ Page
    ... the definition of memory leak is good but the information on ... this is not the most efficient way to allocate the 2-d array. ... You have ROW+1 calls to malloc. ... Allocate ROW pointers. ...
    (comp.lang.c)
  • Re: malloc and maximum size
    ... The malloc function takes size_t type argument. ... malloc allocates much much less memory. ... don't assume that you can allocate as much as SIZE_MAX bytes; ... This program compiles fine with gcc 3.4.3 as ...
    (comp.lang.c)
  • Re: malloc() vs mmap()
    ... virtual memory from the kernel. ... That would explain why it seems to be faster than malloc. ... mallocto allocate memory in your application. ... I'd recommend studying the program's usage patterns. ...
    (comp.unix.programmer)
  • Re: Some performance questions at application level
    ... the time I use 64-bit Windows, so, I'm not that sure what the impact ... allocate a multiple megabyte slab for my kernel buffer pool the ... memory map it and let the OS worry about mass storage accesses. ... I don't use malloc, ...
    (microsoft.public.development.device.drivers)
  • Re: xmalloc string functions
    ... of mallocwhen you are trying to allocate a structure ... caller that fact like I do when malloc() fails to delever me the ... amount of memory I ask it for. ... doesn't have memory to draw stuff it draws. ...
    (comp.lang.c)