Regarding free function

Discussion in 'C Programming' started by Vijay, May 31, 2006.

  1. Vijay

    Vijay Guest

    Hi All,

    I have one question.

    Using free we can release the memory. After releasing memory where this
    released memory will go.. Does it go to back operating system or not?
    Please explain.


    Regards,
    Vijay
     
    Vijay, May 31, 2006
    #1
    1. Advertising

  2. Vijay said:

    > Hi All,
    >
    > I have one question.
    >
    > Using free we can release the memory. After releasing memory where this
    > released memory will go.. Does it go to back operating system or not?


    The Standard says: "The free function causes the space pointed to by ptr to
    be deallocated, that is, made available for further allocation."

    Some people interpret this as meaning that the implementation must hang on
    to the memory in case the program wants it back again... and others don't.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, May 31, 2006
    #2
    1. Advertising

  3. Vijay

    Flash Gordon Guest

    Vijay wrote:
    > Hi All,
    >
    > I have one question.
    >
    > Using free we can release the memory. After releasing memory where this
    > released memory will go.. Does it go to back operating system or not?
    > Please explain.


    On the HS 7000 it goes on a skiing holiday. However, other systems are
    likely to behave differently. It is not required to be returned to the
    OS and on many systems it won't be, instead it will be held in some kind
    of pool of memory easily available for use in subsequent calls to
    malloc/realloc. If you want to know about your system, ask in a group
    dedicated to your system.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, May 31, 2006
    #3
  4. Vijay

    Vijay Guest

    Flash Gordon wrote:
    > Vijay wrote:
    > > Hi All,
    > >
    > > I have one question.
    > >
    > > Using free we can release the memory. After releasing memory where this
    > > released memory will go.. Does it go to back operating system or not?
    > > Please explain.

    >
    > On the HS 7000 it goes on a skiing holiday. However, other systems are
    > likely to behave differently. It is not required to be returned to the
    > OS and on many systems it won't be, instead it will be held in some kind
    > of pool of memory easily available for use in subsequent calls to
    > malloc/realloc. If you want to know about your system, ask in a group
    > dedicated to your system.
    > --
    > Flash Gordon, living in interesting times.
    > Web site - http://home.flash-gordon.me.uk/
    > comp.lang.c posting guidelines and intro:
    > http://clc-wiki.net/wiki/Intro_to_clc


    ok. Is there any way to return released memory back to OS explicitly?
    Regards,
    Vijay
     
    Vijay, Jun 1, 2006
    #4
  5. Vijay said:

    > ok. Is there any way to return released memory back to OS explicitly?


    Call the exit() function, or return from main().

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jun 1, 2006
    #5
  6. Vijay

    CBFalconer Guest

    Richard Heathfield wrote:
    > Vijay said:
    >
    >> ok. Is there any way to return released memory back to OS explicitly?

    >
    > Call the exit() function, or return from main().


    <quibble> On some systems, and if the main from which you return is
    the outer level main, i.e. not a recursive call. The DS9000
    assigns malloced memory from a stock of fusible link PROMs. It
    goes through some gyrations to write that memory more than once.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, Jun 1, 2006
    #6
  7. Vijay

    Flash Gordon Guest

    Richard Heathfield wrote:
    > Vijay said:
    >
    >> ok. Is there any way to return released memory back to OS explicitly?

    >
    > Call the exit() function, or return from main().


    That is return from the original call to main, remember it can be called
    recursively ;-)

    Calling abort should work as well.

    If the OP wants to release memory back to the OS without exiting the
    program, then he will have to go to a group dedicated to his system.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc

    Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
     
    Flash Gordon, Jun 1, 2006
    #7
  8. Flash Gordon said:

    > Richard Heathfield wrote:
    >> Vijay said:
    >>
    >>> ok. Is there any way to return released memory back to OS explicitly?

    >>
    >> Call the exit() function, or return from main().

    >
    > That is return from the original call to main, remember it can be called
    > recursively ;-)


    Yes, I know - I thought of recursive main() /just/ after posting. 'Twas ever
    thus, n'est-ce-pas?

    > Calling abort should work as well.


    Yup. There are also numerous non-9899 methods (e.g. POSIX) for achieving the
    same objective, but let's not go there.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jun 1, 2006
    #8
  9. Vijay

    Flash Gordon Guest

    Richard Heathfield wrote:
    > Flash Gordon said:
    >
    >> Richard Heathfield wrote:
    >>> Vijay said:
    >>>
    >>>> ok. Is there any way to return released memory back to OS explicitly?
    >>> Call the exit() function, or return from main().

    >> That is return from the original call to main, remember it can be called
    >> recursively ;-)

    >
    > Yes, I know - I thought of recursive main() /just/ after posting. 'Twas ever
    > thus, n'est-ce-pas?


    Indeed.

    >> Calling abort should work as well.

    >
    > Yup. There are also numerous non-9899 methods (e.g. POSIX) for achieving the
    > same objective, but let's not go there.


    Yes, but those are off topic here :)
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, Jun 1, 2006
    #9
  10. Vijay

    Jordan Abel Guest

    2006-06-01 <>, CBFalconer wrote:
    > Richard Heathfield wrote:
    >> Vijay said:
    >>
    >>> ok. Is there any way to return released memory back to OS explicitly?

    >>
    >> Call the exit() function, or return from main().

    >
    > <quibble> On some systems, and if the main from which you return is
    > the outer level main, i.e. not a recursive call. The DS9000
    > assigns malloced memory from a stock of fusible link PROMs. It
    > goes through some gyrations to write that memory more than once.


    So how are pointers represented with %p on a DS9k?
     
    Jordan Abel, Jun 1, 2006
    #10
  11. Vijay

    Eric Sosman Guest

    Jordan Abel wrote:

    > 2006-06-01 <>, CBFalconer wrote:
    >
    >>Richard Heathfield wrote:
    >>
    >>>Vijay said:
    >>>
    >>>
    >>>>ok. Is there any way to return released memory back to OS explicitly?
    >>>
    >>>Call the exit() function, or return from main().

    >>
    >><quibble> On some systems, and if the main from which you return is
    >>the outer level main, i.e. not a recursive call. The DS9000
    >>assigns malloced memory from a stock of fusible link PROMs. It
    >>goes through some gyrations to write that memory more than once.

    >
    >
    > So how are pointers represented with %p on a DS9k?


    Every pointer value has the printed representation

    "You are in a twisty little maze of passages, all different."

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 2, 2006
    #11
  12. Vijay

    Jordan Abel Guest

    2006-06-02 <>, Eric Sosman wrote:
    > Jordan Abel wrote:
    >
    >> 2006-06-01 <>, CBFalconer wrote:
    >>
    >>>Richard Heathfield wrote:
    >>>
    >>>>Vijay said:
    >>>>
    >>>>
    >>>>>ok. Is there any way to return released memory back to OS explicitly?
    >>>>
    >>>>Call the exit() function, or return from main().
    >>>
    >>><quibble> On some systems, and if the main from which you return is
    >>>the outer level main, i.e. not a recursive call. The DS9000
    >>>assigns malloced memory from a stock of fusible link PROMs. It
    >>>goes through some gyrations to write that memory more than once.

    >>
    >>
    >> So how are pointers represented with %p on a DS9k?

    >
    > Every pointer value has the printed representation
    >
    > "You are in a twisty little maze of passages, all different."


    And how am I supposed to translate those back to the original pointer?
     
    Jordan Abel, Jun 2, 2006
    #12
  13. Jordan Abel wrote:
    > 2006-06-02 <>, Eric Sosman wrote:
    > > Jordan Abel wrote:
    > >
    > >> 2006-06-01 <>, CBFalconer wrote:
    > >>
    > >>>Richard Heathfield wrote:
    > >>>
    > >>>>Vijay said:
    > >>>>
    > >>>>
    > >>>>>ok. Is there any way to return released memory back to OS explicitly?
    > >>>>
    > >>>>Call the exit() function, or return from main().
    > >>>
    > >>><quibble> On some systems, and if the main from which you return is
    > >>>the outer level main, i.e. not a recursive call. The DS9000
    > >>>assigns malloced memory from a stock of fusible link PROMs. It
    > >>>goes through some gyrations to write that memory more than once.
    > >>
    > >>
    > >> So how are pointers represented with %p on a DS9k?

    > >
    > > Every pointer value has the printed representation
    > >
    > > "You are in a twisty little maze of passages, all different."

    >
    > And how am I supposed to translate those back to the original pointer?


    You're not.

    Robert Gamble
     
    Robert Gamble, Jun 2, 2006
    #13
  14. Vijay

    Jordan Abel Guest

    2006-06-02 <>, Robert Gamble wrote:
    > Jordan Abel wrote:
    >> 2006-06-02 <>, Eric Sosman wrote:
    >> > Jordan Abel wrote:
    >> >
    >> >> 2006-06-01 <>, CBFalconer wrote:
    >> >>
    >> >>>Richard Heathfield wrote:
    >> >>>
    >> >>>>Vijay said:
    >> >>>>
    >> >>>>
    >> >>>>>ok. Is there any way to return released memory back to OS explicitly?
    >> >>>>
    >> >>>>Call the exit() function, or return from main().
    >> >>>
    >> >>><quibble> On some systems, and if the main from which you return is
    >> >>>the outer level main, i.e. not a recursive call. The DS9000
    >> >>>assigns malloced memory from a stock of fusible link PROMs. It
    >> >>>goes through some gyrations to write that memory more than once.
    >> >>
    >> >>
    >> >> So how are pointers represented with %p on a DS9k?
    >> >
    >> > Every pointer value has the printed representation
    >> >
    >> > "You are in a twisty little maze of passages, all different."

    >>
    >> And how am I supposed to translate those back to the original pointer?

    >
    > You're not.
    >
    > Robert Gamble


    Then you don't have a conforming implementation.
     
    Jordan Abel, Jun 2, 2006
    #14
  15. Jordan Abel wrote:
    > 2006-06-02 <>, Robert Gamble wrote:
    > > Jordan Abel wrote:
    > >> 2006-06-02 <>, Eric Sosman wrote:
    > >> > Jordan Abel wrote:
    > >> >
    > >> >> 2006-06-01 <>, CBFalconer wrote:
    > >> >>
    > >> >>>Richard Heathfield wrote:
    > >> >>>
    > >> >>>>Vijay said:
    > >> >>>>
    > >> >>>>
    > >> >>>>>ok. Is there any way to return released memory back to OS explicitly?
    > >> >>>>
    > >> >>>>Call the exit() function, or return from main().
    > >> >>>
    > >> >>><quibble> On some systems, and if the main from which you return is
    > >> >>>the outer level main, i.e. not a recursive call. The DS9000
    > >> >>>assigns malloced memory from a stock of fusible link PROMs. It
    > >> >>>goes through some gyrations to write that memory more than once.
    > >> >>
    > >> >>
    > >> >> So how are pointers represented with %p on a DS9k?
    > >> >
    > >> > Every pointer value has the printed representation
    > >> >
    > >> > "You are in a twisty little maze of passages, all different."
    > >>
    > >> And how am I supposed to translate those back to the original pointer?

    > >
    > > You're not.
    > >
    > > Robert Gamble

    >
    > Then you don't have a conforming implementation.


    I did not realize that the Standard made such a guarantee but as I am
    reviewing the section detailing the fscanf function I see that it does.
    I am slightly surprised.

    Robert Gamble
     
    Robert Gamble, Jun 2, 2006
    #15
  16. Vijay

    Flash Gordon Guest

    Robert Gamble wrote:
    > Jordan Abel wrote:
    >> 2006-06-02 <>, Robert Gamble wrote:
    >>> Jordan Abel wrote:
    >>>> 2006-06-02 <>, Eric Sosman wrote:
    >>>>> Jordan Abel wrote:


    <snip>

    >>>>>> So how are pointers represented with %p on a DS9k?
    >>>>> Every pointer value has the printed representation
    >>>>>
    >>>>> "You are in a twisty little maze of passages, all different."
    >>>> And how am I supposed to translate those back to the original pointer?
    >>> You're not.
    >>>
    >>> Robert Gamble

    >> Then you don't have a conforming implementation.

    >
    > I did not realize that the Standard made such a guarantee but as I am
    > reviewing the section detailing the fscanf function I see that it does.
    > I am slightly surprised.


    However, Jordan is still not expected to translate it back. Only the
    implementation is, and as long as it can manage it (for example keeping
    a table of what textual representation it has use for what pointer) it
    can use any representation it likes.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, Jun 2, 2006
    #16
  17. Vijay

    Jordan Abel Guest

    2006-06-02 <-gordon.me.uk>, Flash Gordon wrote:
    > Robert Gamble wrote:
    >> Jordan Abel wrote:
    >>> 2006-06-02 <>, Robert Gamble wrote:
    >>>> Jordan Abel wrote:
    >>>>> 2006-06-02 <>, Eric Sosman wrote:
    >>>>>> Jordan Abel wrote:

    >
    > <snip>
    >
    >>>>>>> So how are pointers represented with %p on a DS9k?
    >>>>>> Every pointer value has the printed representation
    >>>>>>
    >>>>>> "You are in a twisty little maze of passages, all different."
    >>>>> And how am I supposed to translate those back to the original pointer?
    >>>> You're not.
    >>>>
    >>>> Robert Gamble
    >>> Then you don't have a conforming implementation.

    >>
    >> I did not realize that the Standard made such a guarantee but as I am
    >> reviewing the section detailing the fscanf function I see that it does.
    >> I am slightly surprised.

    >
    > However, Jordan is still not expected to translate it back. Only the
    > implementation is, and as long as it can manage it (for example keeping
    > a table of what textual representation it has use for what pointer) it
    > can use any representation it likes.


    But it can't use the exact same representation for two different pointers.

    Note that I did not say it has to have any relationship to the
    representation in memory - it can store the pointers in a table and
    print an index into that table.

    However, unless you subscribe to the "evil padding bits" theory, it
    _also_ has to be legal to take the address of the pointer, cast to
    pointer to unsigned char, and print sizeof(void*) bytes as a hex dump,
    and then copy those bytes back.
     
    Jordan Abel, Jun 2, 2006
    #17
  18. Vijay

    Flash Gordon Guest

    Jordan Abel wrote:
    > 2006-06-02 <-gordon.me.uk>, Flash Gordon wrote:
    >> Robert Gamble wrote:
    >>> Jordan Abel wrote:
    >>>> 2006-06-02 <>, Robert Gamble wrote:
    >>>>> Jordan Abel wrote:
    >>>>>> 2006-06-02 <>, Eric Sosman wrote:
    >>>>>>> Jordan Abel wrote:

    >> <snip>
    >>
    >>>>>>>> So how are pointers represented with %p on a DS9k?
    >>>>>>> Every pointer value has the printed representation
    >>>>>>>
    >>>>>>> "You are in a twisty little maze of passages, all different."
    >>>>>> And how am I supposed to translate those back to the original pointer?
    >>>>> You're not.
    >>>>>
    >>>>> Robert Gamble
    >>>> Then you don't have a conforming implementation.
    >>> I did not realize that the Standard made such a guarantee but as I am
    >>> reviewing the section detailing the fscanf function I see that it does.
    >>> I am slightly surprised.

    >> However, Jordan is still not expected to translate it back. Only the
    >> implementation is, and as long as it can manage it (for example keeping
    >> a table of what textual representation it has use for what pointer) it
    >> can use any representation it likes.

    >
    > But it can't use the exact same representation for two different pointers.


    True.

    > Note that I did not say it has to have any relationship to the
    > representation in memory - it can store the pointers in a table and
    > print an index into that table.
    >
    > However, unless you subscribe to the "evil padding bits" theory, it
    > _also_ has to be legal to take the address of the pointer, cast to
    > pointer to unsigned char, and print sizeof(void*) bytes as a hex dump,
    > and then copy those bytes back.


    What if the padding bits are only mischievous, not actually evil? ;-)

    Actually, the standard does not define padding bits for a pointer, only
    for integer types. So I believe that all bits in the pointer are part of
    the representation and therefore unless you call free on that pointer
    (which could make the representation become a trap) I believe using an
    unsigned char pointer to save the representation to a file and later
    reading it back and using it would be perfectly legal. However, remember
    that different pointer types could be different sizes, so if you want to
    use sizeof(void*) you should first assign the pointer to a void* variable.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, Jun 2, 2006
    #18
  19. Vijay

    Eric Sosman Guest

    Jordan Abel wrote:
    > 2006-06-02 <-gordon.me.uk>, Flash Gordon wrote:
    >
    >>Robert Gamble wrote:
    >>
    >>>Jordan Abel wrote:
    >>>
    >>>>2006-06-02 <>, Robert Gamble wrote:
    >>>>
    >>>>>Jordan Abel wrote:
    >>>>>
    >>>>>>2006-06-02 <>, Eric Sosman wrote:
    >>>>>>
    >>>>>>>Jordan Abel wrote:

    >>
    >><snip>
    >>
    >>>>>>>>So how are pointers represented with %p on a DS9k?
    >>>>>>>
    >>>>>>> Every pointer value has the printed representation
    >>>>>>>
    >>>>>>> "You are in a twisty little maze of passages, all different."
    >>>>>>
    >>>>>>And how am I supposed to translate those back to the original pointer?
    >>>>>
    >>>>>You're not.
    >>>>>
    >>>>>Robert Gamble
    >>>>
    >>>>Then you don't have a conforming implementation.
    >>>
    >>>I did not realize that the Standard made such a guarantee but as I am
    >>>reviewing the section detailing the fscanf function I see that it does.
    >>> I am slightly surprised.

    >>
    >>However, Jordan is still not expected to translate it back. Only the
    >>implementation is, and as long as it can manage it (for example keeping
    >>a table of what textual representation it has use for what pointer) it
    >>can use any representation it likes.

    >
    >
    > But it can't use the exact same representation for two different pointers.


    It's difficult to reproduce DS9000 output on Usenet, which
    may be why you seem to have overlooked the many font changes in
    the fifty-nine characters of the printed representation. The
    characters themselves are "carrier" rather than "signal," and
    it's the font changes that encode the pointer value. This is
    the same scheme Francis Bacon used to encode his secret messages
    in the First Folio of the works of the so-called "Shakespeare."

    ;-)

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 2, 2006
    #19
  20. Vijay

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Eric Sosman wrote:
    > Jordan Abel wrote:
    >
    >> 2006-06-01 <>, CBFalconer wrote:
    >>
    >>> Richard Heathfield wrote:
    >>>
    >>>> Vijay said:
    >>>>
    >>>>
    >>>>> ok. Is there any way to return released memory back to OS explicitly?
    >>>>
    >>>> Call the exit() function, or return from main().
    >>>
    >>> <quibble> On some systems, and if the main from which you return is
    >>> the outer level main, i.e. not a recursive call. The DS9000
    >>> assigns malloced memory from a stock of fusible link PROMs. It
    >>> goes through some gyrations to write that memory more than once.

    >>
    >>
    >> So how are pointers represented with %p on a DS9k?

    >
    > Every pointer value has the printed representation
    >
    > "You are in a twisty little maze of passages, all different."


    Eric, ISTR that the NULL pointer was treated differently.
    Notwithstanding your comment about font changes as information and text
    as carrier, on my old DS9000, the NULL pointer has a "%p" printed
    representation of

    "You are in a maze of twisty little passages, all alike."

    ;-)



    - --

    Lew Pitcher, IT Specialist, Corporate Technology Solutions,
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.3 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFEgCztagVFX4UWr64RAn1FAJ4+BHxzwHWalGy7SOci2aauU2UQQQCglWvq
    cMwlqMH1o5yVWUldZiplS+I=
    =HKRZ
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Jun 2, 2006
    #20
    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. Replies:
    19
    Views:
    562
    Dave Thompson
    Jan 3, 2007
  2. Panduranga Chary

    How memory function free() knows how much memory to free.

    Panduranga Chary, Dec 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    417
    Keith Thompson
    Dec 27, 2007
  3. george
    Replies:
    0
    Views:
    1,142
    george
    Aug 29, 2008
  4. mohammed_a_o
    Replies:
    0
    Views:
    283
    mohammed_a_o
    Nov 30, 2010
  5. paolo

    regarding free function in c library

    paolo, Dec 17, 2011, in forum: C Programming
    Replies:
    23
    Views:
    734
    Keith Thompson
    Dec 19, 2011
Loading...

Share This Page