Dynamic allocate memory problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Franklin Lee

    Dynamic allocate memory problem

    Hi All,

    I use new to allocate some memory,even I doesn't use delete to release them.
    When my Application exit, OS will release them.

    Am I right?

    If I'm right, how about Thread especally on Solaries OS?
    This means that I use new to allocate memory in one Thread and doesn't use
    delete to release them.
    When this Thread dies, OS will release them?

    Thank you so much!

    PS: which book can I refer?

    Franklin


  • Gianni Mariani

    #2
    Re: Dynamic allocate memory problem

    Franklin Lee wrote:[color=blue]
    > Hi All,
    >
    > I use new to allocate some memory,even I doesn't use delete to release them.
    > When my Application exit, OS will release them.
    >
    > Am I right?[/color]

    No. It depends on your system. This is true for most modern
    non-embedded operating systems.
    [color=blue]
    >
    > If I'm right, how about Thread especally on Solaries OS?
    > This means that I use new to allocate memory in one Thread and doesn't use
    > delete to release them.
    > When this Thread dies, OS will release them?[/color]

    No. Memory is shared between threads and solaris does not know which
    thread allocated which memory.

    Comment

    • Dave Townsend

      #3
      Re: Dynamic allocate memory problem

      Well, you "are right" , that is when your
      application exits, the memory and other resources are automatically freed
      by the operating system. However, this is NOT the important point....


      Generally, in C++ you should dispose of memory you've allocated once you've
      performed the operations for which you allocated it, that way you can reduce
      the "high water mark" enabling you to handle larger amounts of data without
      running out of virtual memory. Consider the situation such as a GUI like
      MS
      Word or Internet Exploder , you potentially could open, edit and close
      multiple documents and have
      the same application running for several days at a time. If memory wasn't
      properly deallocated
      once a document had been closed ( for instance), the app would eventually
      consume all the virtual memory on the machine and would eventually crash, do
      undefined things,
      etc. Even with a simple "batch" type application, it is important to
      identify the points in
      the flow of code where allocated memory can be safely disposed, this way the
      application
      could potentially handle a larger data set with the same physical/virtual
      resources available
      on the computer.

      Of course, if your app is sooooo simple that it just spits out some output
      and quits, then this
      is probably not an issue, although I would learn the habit of correctly
      managing memory as
      a professional skill to do as a matter of routine so you don't have to
      revisit the code in the
      future.

      Threads have nothing to do with memory management (allocation/deallocation).
      Memory can be allocated by one thread and deleted by another thread, there
      are no requirements regarding disposing
      of memory, you have to program that into your code. As a general rule in a
      C++ program, wherever
      you allocate memory using new ( or home brewed memory allocator), there must
      be a corresponding
      delete(s) somewhere to deallocate it, if you don't got that, you got a
      memory leak. Whenever I see a
      memory allocation such as malloc or new, I want to assure myself that there
      is a correct delete/free
      operation happening in the normal flow of control. Better still, be sure
      there's a correct delete/free in
      the C++ exception flow. Constructors/destructors are very handy for
      correctly & automatically managing memory allocation/deallocation in C++
      programs. Poor guys like me, had to do this
      by pure brain-power before C++ came along, with C++ you've got it easy!

      Tools such as Purify can help identify memory leaks after the fact, but in
      my opinion its far
      easier to bake the memory management into the original code. I'd add that
      I've encountered a number of
      "killer" bugs in production applications which were due to memory leaks, so
      get into the
      habit of correct memory management as a career advancement strategy. When
      you allocate, figure
      out where you need to deallocate.

      I don't know which book you can look at, this is nothing to do with threads,
      any good book
      on C++ should drill in "initializa tion is resource allocation" and go from
      there.

      dave


      "Franklin Lee" <pengtaoli@hotm ail.com> wrote in message
      news:c895be$eqp @netnews.proxy. lucent.com...[color=blue]
      > Hi All,
      >
      > I use new to allocate some memory,even I doesn't use delete to release[/color]
      them.[color=blue]
      > When my Application exit, OS will release them.
      >
      > Am I right?
      >
      > If I'm right, how about Thread especally on Solaries OS?
      > This means that I use new to allocate memory in one Thread and doesn't use
      > delete to release them.
      > When this Thread dies, OS will release them?
      >
      > Thank you so much!
      >
      > PS: which book can I refer?
      >
      > Franklin
      >
      >[/color]


      Comment

      • Franklin Lee

        #4
        Re: Dynamic allocate memory problem

        Hi Gianni and ALL,

        I believe that you are right.

        But which books could I find the detail information?

        Thank you so much!

        Franklin



        Comment

        • JKop

          #5
          Re: Dynamic allocate memory problem

          Franklin Lee posted:
          [color=blue]
          > Hi All,
          >
          > I use new to allocate some memory,even I doesn't use delete to release
          > them. When my Application exit, OS will release them.
          >
          > Am I right?[/color]


          No disrepect to you, but you're like a child that plays with lego and then
          leaves it thrown all over the room; but what do you care? - Mammy will clean
          them up.

          One thing though, in the C++ Standard, there's no guarantee that Mammy will
          clean it up.


          I suggest you put your hand in the fire and put a $1 in the "Leak Box" every
          time you use "new" and then *don't* use "delete". Then rhyme this off to
          yourself 100 times:


          Where I use new , I must use delete
          Where I use new , I must use delete
          Where I use new , I must use delete
          Where I use new , I must use delete
          Where I use new , I must use delete
          Where I use new , I must use delete
          Where I use new , I must use delete
          Where I use new , I must use delete



          -JKop

          Comment

          Working...