what to do when malloc returns NULL?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Michaux

    what to do when malloc returns NULL?

    Suppose I have implemented a language with garbage collection in C. I
    have wrapped malloc in my own C function. If malloc returns NULL then
    I can run the garbage collector and then try malloc again. What do I
    do if malloc returns NULL again? Can a C program ask the operating
    system for more space and then I could try malloc a third time?

    Thanks,
    Peter
  • Harald van =?UTF-8?b?RMSzaw==?=

    #2
    Re: what to do when malloc returns NULL?

    On Mon, 24 Mar 2008 22:39:25 -0700, Peter Michaux wrote:
    Suppose I have implemented a language with garbage collection in C. I
    have wrapped malloc in my own C function. If malloc returns NULL then I
    can run the garbage collector and then try malloc again. What do I do if
    malloc returns NULL again? Can a C program ask the operating system for
    more space and then I could try malloc a third time?
    No, or at least, not portably, and I'm not aware of any system-specific
    ways. If malloc returns NULL, and you've freed everything that can be
    freed, there just isn't enough memory available. One way to handle this
    is abort (either the program, or the current operation), and show the
    user an error. Another is to pause, asking the user to shut down other
    applications. But in the second case, that's not something that the OS
    should do automatically.

    Comment

    • Jack.Thomson.v3@gmail.com

      #3
      Re: what to do when malloc returns NULL?

      Pausing for some time and trying to allocate memory could work, if in
      the mean time some memory gets freed.

      ~Jack
      -------------------------------------



      On Mar 25, 10:39 am, Peter Michaux <petermich...@g mail.comwrote:
      Suppose I have implemented a language with garbage collection in C.  I
      have wrapped malloc in my own C function. If malloc returns NULL then
      I can run the garbage collector and then try malloc again. What do I
      do if malloc returns NULL again? Can a C program ask the operating
      system for more space and then I could try malloc a third time?
      >
      Thanks,
      Peter

      Comment

      • Peter Michaux

        #4
        Re: what to do when malloc returns NULL?

        On Mar 24, 10:46 pm, Harald van D©¦k <true...@gmail. comwrote:
        On Mon, 24 Mar 2008 22:39:25 -0700, Peter Michaux wrote:
        Suppose I have implemented a language with garbage collection in C. I
        have wrapped malloc in my own C function. If malloc returns NULL then I
        can run the garbage collector and then try malloc again. What do I do if
        malloc returns NULL again? Can a C program ask the operating system for
        more space and then I could try malloc a third time?
        >
        No, or at least, not portably, and I'm not aware of any system-specific
        ways.
        Well I suppose that is good news in some way. If it is not possible
        then don't have to implement it ;-)

        Thanks,
        Peter

        Comment

        • santosh

          #5
          Re: what to do when malloc returns NULL?

          Peter Michaux wrote:
          Suppose I have implemented a language with garbage collection in C. I
          have wrapped malloc in my own C function.
          If malloc returns NULL then
          I can run the garbage collector and then try malloc again. What do I
          do if malloc returns NULL again? Can a C program ask the operating
          system for more space and then I could try malloc a third time?
          The only way that a portable C program can ask the operating system for
          more space for dynamic memory is through the *alloc functions. So you
          _could_ keep trying malloc a predefined number of times before bombing
          out, but IMHO it's better, in a new language, to use the APIs provided
          by the platform for memory management. Often they provide control
          beyond what ISO C malloc does.

          Comment

          • santosh

            #6
            Re: what to do when malloc returns NULL?

            santosh wrote:
            Peter Michaux wrote:
            >
            >Suppose I have implemented a language with garbage collection in C.
            >I have wrapped malloc in my own C function.
            >If malloc returns NULL then
            >I can run the garbage collector and then try malloc again. What do I
            >do if malloc returns NULL again? Can a C program ask the operating
            >system for more space and then I could try malloc a third time?
            >
            The only way that a portable C program can ask the operating system
            for more space for dynamic memory is through the *alloc functions. So
            you _could_ keep trying malloc a predefined number of times before
            bombing out, but IMHO it's better, in a new language, to use the APIs
            provided by the platform for memory management. Often they provide
            control beyond what ISO C malloc does.
            OK, seeing Harald's reply I see that I misunderstood your question.
            Please excuse.

            Comment

            • CBFalconer

              #7
              Re: what to do when malloc returns NULL?

              Peter Michaux wrote:
              Harald van D©¦k <true...@gmail. comwrote:
              >Peter Michaux wrote:
              >>
              >>Suppose I have implemented a language with garbage collection in
              >>C. I have wrapped malloc in my own C function. If malloc returns
              >>NULL then I can run the garbage collector and then try malloc
              >>again. What do I do if malloc returns NULL again? Can a C program
              >>ask the operating system for more space and then I could try
              >>malloc a third time?
              >>
              >No, or at least, not portably, and I'm not aware of any
              >system-specific ways.
              >
              Well I suppose that is good news in some way. If it is not possible
              then don't have to implement it ;-)
              This illustrates the problems involved in using garbage collection,
              which is not what the C system was built to use. There is no way
              it can be as efficient as normal use of malloc/free. Which means
              you have to pay attention.

              --
              [mail]: Chuck F (cbfalconer at maineline dot net)
              [page]: <http://cbfalconer.home .att.net>
              Try the download section.



              --
              Posted via a free Usenet account from http://www.teranews.com

              Comment

              Working...