Re: More questions on realloc



On Tue, 29 Nov 2005 17:27:48 GMT, in comp.lang.c , Robert Harris
<robert.f.harris@xxxxxxxxxxxxxxxx> wrote:

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

This is sometimes true, but not relevant to the OP since
a) you don't know if your compiler can do this and
b) you don't know if it did, in any specific case and
c) it may in any events not be able to, in any specific case.

Whatever, realloc can under the hood operate however it likes,
provided it behaves *as if* it had done a new *alloc, copy and free.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
.



Relevant Pages

  • Re: malloc/free question
    ... I'd like to be able to malloc a block of memory, ... For example, malloc 100 ... Use realloc. ... seriously consider using two lists. ...
    (comp.lang.c)
  • Re: [OT]Re: malloc
    ... so all malloc calls *always* return a pointer to memory. ... realloc() or reallocfwill be ... initialization only happens once for each byte, ...
    (comp.lang.c)
  • Re: malloc/free question
    ... I'd like to be able to malloc a block of memory, ... For example, malloc 100 ... Use realloc. ... guaranteed that a reallocrequesting a smaller size will succeed; ...
    (comp.lang.c)
  • Re: Querry (Newbe)
    ... > If we do the realloc then it means that we have allocated the ... > extended memory for the current memory, ... > which I extendend to realloc if compiler allocates memory ... > what is the diffrence between the callocand malloc() ...
    (comp.lang.c)
  • Re: How to get the memory block size which allocated by new operator?
    ... memory that not released.When allocate memory by new operator,I will ... set of memory management routines that are far superiour to malloc, realloc, ... Programmers know when they're combining multiple outputs into a single ... NULL is stuffed into pbBuf when realloc returns, destroying the only pointer ...
    (microsoft.public.vc.language)