how the realloc( ) allocates memory ?

Discussion in 'C Programming' started by M a n i s h, Nov 21, 2003.

  1. M a n i s h

    M a n i s h Guest

    is there any difference in how the realloc() reallocates memory to
    varibles which were being allocated memory using malloc() and calloc ?
     
    M a n i s h, Nov 21, 2003
    #1
    1. Advertising

  2. M a n i s h <> scribbled the following:
    > is there any difference in how the realloc() reallocates memory to
    > varibles which were being allocated memory using malloc() and calloc ?


    This would have to be an implementation-specific detail.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "We sorcerers don't like to eat our words, so to say."
    - Sparrowhawk
     
    Joona I Palaste, Nov 21, 2003
    #2
    1. Advertising

  3. Joona I Palaste <> wrote:
    >M a n i s h <> scribbled the following:
    >> is there any difference in how the realloc() reallocates memory to
    >> varibles which were being allocated memory using malloc() and calloc ?

    >
    >This would have to be an implementation-specific detail.


    Is that really true, in respect to what the OP is asking?

    The realloc, calloc and malloc functions all perform "magic"
    behind the scenes, and the details are not available to the
    programmer. But outwardly, "magic" is spelled the same for all
    three.

    Both the realloc and free functions can operate on a pointer
    returned by any of the three. I think that is what the OP
    probably meant by "is there any difference", and for the
    programmer the answer is no, they are the same.

    Of course, underneath they might accomplish "magic" in three
    distinct ways... but that is of concern only to the implementor.

    --
    Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
    Ukpeagvik (Barrow, Alaska)
     
    Floyd Davidson, Nov 21, 2003
    #3
  4. M a n i s h

    Richard Bos Guest

    (M a n i s h) wrote:

    > is there any difference in how the realloc() reallocates memory to
    > varibles which were being allocated memory using malloc() and calloc ?


    Internally, no idea. But it doesn't matter, since the effect is
    identical - you are either given a pointer to the new memory, or a null
    pointer.

    Richard
     
    Richard Bos, Nov 21, 2003
    #4
  5. On Fri, 21 Nov 2003 00:59:54 -0900, Floyd Davidson wrote:

    > Joona I Palaste <> wrote:
    >>M a n i s h <> scribbled the following:
    >>> is there any difference in how the realloc() reallocates memory to
    >>> varibles which were being allocated memory using malloc() and calloc ?

    >>
    >>This would have to be an implementation-specific detail.

    >
    > Is that really true, in respect to what the OP is asking?
    >
    > The realloc, calloc and malloc functions all perform "magic"
    > behind the scenes, and the details are not available to the
    > programmer. But outwardly, "magic" is spelled the same for all
    > three.
    >
    > Both the realloc and free functions can operate on a pointer
    > returned by any of the three. I think that is what the OP
    > probably meant by "is there any difference", and for the
    > programmer the answer is no, they are the same.
    >
    > Of course, underneath they might accomplish "magic" in three
    > distinct ways... but that is of concern only to the implementor.


    I think what the OP was interested in may have been:

    Q: Does realloc treat a pointer to calloced memory different from malloced
    memory, will the "new part" of a calloced array that grows be zeroed?

    A: An implementation could do this, but the standard doesn't say it
    should, so for portable code you can't depend on it.

    OP:
    You could write your own clearing realloc (crealloc?) like this:

    crealloc (void *,size_t new_size, size_t current_size)

    That calls realloc behind the scene and then memsets the new portion of
    the array.



    --
    NPV

    "the large print giveth, and the small print taketh away"
    Tom Waits - Step right up
     
    Nils Petter Vaskinn, Nov 21, 2003
    #5
  6. > Is that really true, in respect to what the OP is asking?

    I suppose OP stands for Original Poster.
     
    Vijay Kumar R Zanvar, Nov 21, 2003
    #6
  7. "Vijay Kumar R Zanvar" <> wrote:
    >> Is that really true, in respect to what the OP is asking?

    >
    >I suppose OP stands for Original Poster.


    Yes.

    --
    Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
    Ukpeagvik (Barrow, Alaska)
     
    Floyd Davidson, Nov 21, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonas
    Replies:
    21
    Views:
    1,172
    Mark McIntyre
    Oct 14, 2003
  2. Jeff Rodriguez

    Can realloc potentially cause a memory leak?

    Jeff Rodriguez, Nov 19, 2003, in forum: C Programming
    Replies:
    4
    Views:
    346
    Derk Gwen
    Nov 19, 2003
  3. Marlene Stebbins

    Memory leaks with realloc()

    Marlene Stebbins, Feb 23, 2005, in forum: C Programming
    Replies:
    7
    Views:
    790
    Marlene Stebbins
    Feb 25, 2005
  4. Asbjørn Sæbø

    Macro that allocates storage and "returns" value

    Asbjørn Sæbø, Jan 9, 2008, in forum: C Programming
    Replies:
    12
    Views:
    540
    Mark Bluemel
    Jan 11, 2008
  5. DDD
    Replies:
    3
    Views:
    505
    James Kanze
    Jul 22, 2009
Loading...

Share This Page