Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ heap allocation question

C++ heap allocation question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KellyR
    wrote on last edited by
    #1

    Maybe a dumb question, but I was just curious -- are heap-allocated objects in C++ destroyed when a program is exited even if they aren't explicitly freed with calls to delete or free? Thanks!

    KR

    M J 2 Replies Last reply
    0
    • K KellyR

      Maybe a dumb question, but I was just curious -- are heap-allocated objects in C++ destroyed when a program is exited even if they aren't explicitly freed with calls to delete or free? Thanks!

      KR

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Yes - all the memory your process used is returned to the system. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      1 Reply Last reply
      0
      • K KellyR

        Maybe a dumb question, but I was just curious -- are heap-allocated objects in C++ destroyed when a program is exited even if they aren't explicitly freed with calls to delete or free? Thanks!

        KR

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #3

        To be more specific; the heap is destroyed, but the destructor is not called for objects. A non-issue the vast majority of the time (handles will also be closed) but there may be a rare circumstance where a resource needs to be closed a more complex way. (This is rare enough that I can't even think of a non-contrived example.)

        Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

        M J 2 Replies Last reply
        0
        • J Joe Woodbury

          To be more specific; the heap is destroyed, but the destructor is not called for objects. A non-issue the vast majority of the time (handles will also be closed) but there may be a rare circumstance where a resource needs to be closed a more complex way. (This is rare enough that I can't even think of a non-contrived example.)

          Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Joe Woodbury wrote:

          o be more specific; the heap is destroyed, but the destructor is not called for objects.

          Good point! Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          • J Joe Woodbury

            To be more specific; the heap is destroyed, but the destructor is not called for objects. A non-issue the vast majority of the time (handles will also be closed) but there may be a rare circumstance where a resource needs to be closed a more complex way. (This is rare enough that I can't even think of a non-contrived example.)

            Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            Actually, a non-contrived answer was staring me in the face: a file class that caches writes.

            Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups