Re: More questions on realloc



Mark McIntyre wrote:
On Tue, 29 Nov 2005 17:01:17 GMT, in comp.lang.c , "James S.
Singleton" <pt109@xxxxxxxxxx> wrote:


My question is, how does realloc know that p points to a memory region
size1 bytes in length?


[snip]

Or, to put it differently, can realloc be
implemented with malloc, and no other memory management call?


You'd need memcpy and free. How else can you copy the memory from the
old to the new pointers, and then free the old one?


But realloc doesn't always have to reallocate memory; if you malloc, say, 10 bytes, you may well actually get 32 (or 1024) bytes. Your C library knows how much but you don't, so calling realloc() is often a win over malloc(), memcpy() and free().

Robert
.



Relevant Pages

  • Re: Cached file read performance
    ... 50000 malloc in 0.5367s ... 50000 memcpy of 8192 byte blocks in 1.0887s ... printf("out of memory initializing destination array!\n"); ... numblocks, blocksz, elapsed); ...
    (freebsd-performance)
  • Re: Ada memory management seems slow
    ... >> GNAT uses malloc and free from the C library for memory management - so ... >> performace is the same as with C. ... Where you find almost the full sources for all gnat library functions. ...
    (comp.lang.ada)
  • Re: segmentation fault on calloc
    ... >> documentation, that's equally important to code quality when dealing ... seen lots of crashes in malloc, calloc and free, and all of them were ... the end of an allocated block, it is almost certain to overwrite some ... to a memory management function (no other code is allowed to touch ...
    (comp.unix.programmer)
  • Re: ANN: Q language website (new)
    ... "Garbage Collection Can Be Faster Than Stack Allocation" ... > the necessary skills to optimize their use of malloc and free and know ... Same with C++ developers in their ... > mixing different methods of memory management is not a good idea (a la ...
    (comp.lang.misc)
  • Re: ANN: Q language website (new)
    ... >> the necessary skills to optimize their use of malloc and free and know ... (more specifically programs written in other programming languages). ... >> mixing different methods of memory management is not a good idea (a la ... >> applications using memory upwards in the hundreds of megabytes, ...
    (comp.lang.misc)