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. VERY basic memory question

VERY basic memory question

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancequestion
8 Posts 5 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.
  • _ Offline
    _ Offline
    __DanC__
    wrote on last edited by
    #1

    I have a really simple question which I cannot seem to find the answer to... If I allocate memory like this: char * pArray = (char*)malloc(123); Then I free it with: free(pArray); And the same with: char * pArray = new char[123]; And delete[] pArray; But how would I free an array created like: WCHAR pwszRealPath[MAX_PATH]; The best answer I can find is that I don't but I would like to know why.

    T 1 Reply Last reply
    0
    • _ __DanC__

      I have a really simple question which I cannot seem to find the answer to... If I allocate memory like this: char * pArray = (char*)malloc(123); Then I free it with: free(pArray); And the same with: char * pArray = new char[123]; And delete[] pArray; But how would I free an array created like: WCHAR pwszRealPath[MAX_PATH]; The best answer I can find is that I don't but I would like to know why.

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      __DanC__ wrote:

      how would I free an array created like: WCHAR pwszRealPath[MAX_PATH];

      such an array is created on the stack, and not on the heap. so basically, no need to free it ;) it will be destroyed when going out of scope, of at program termination if static/global...

      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      _ 1 Reply Last reply
      0
      • T toxcct

        __DanC__ wrote:

        how would I free an array created like: WCHAR pwszRealPath[MAX_PATH];

        such an array is created on the stack, and not on the heap. so basically, no need to free it ;) it will be destroyed when going out of scope, of at program termination if static/global...

        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        _ Offline
        _ Offline
        __DanC__
        wrote on last edited by
        #3

        Thanks, that is what I thought would be the case but wanted to clarify in case I go causing any memory leaks!

        T C 2 Replies Last reply
        0
        • _ __DanC__

          Thanks, that is what I thought would be the case but wanted to clarify in case I go causing any memory leaks!

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          to be very clear here is what happens with this code:

          char* pArray = new char[123];

          a variable pArray of type pointer to char is created on the stack. then, a block of 123 chars is allocated on the heap. the address of the just allocated memory is stored into the pointer variable you have on the stack (pArray). so, if you put such a code into a scope, and if you don't free the memory allocated before going out of scope, the variable on the stack is destroyed, but not the memory block on the heap. so you just loose the reference (the address) to your allocated memory on the heap, hence the memory leak...

          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          1 Reply Last reply
          0
          • _ __DanC__

            Thanks, that is what I thought would be the case but wanted to clarify in case I go causing any memory leaks!

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            To make things even more simple, there's only one little rule to remember: everything that is allocated with new should be deleted using delete; everything that is allocated with new...[] should be deleted using delete[] and everything that is allocated with malloc should be deleted with free. For the rest, you don't have to care (and Toxcct explained that a bit more in details).

            Cédric Moonen Software developer
            Charting control [v1.4] OpenGL game tutorial in C++

            J 1 Reply Last reply
            0
            • C Cedric Moonen

              To make things even more simple, there's only one little rule to remember: everything that is allocated with new should be deleted using delete; everything that is allocated with new...[] should be deleted using delete[] and everything that is allocated with malloc should be deleted with free. For the rest, you don't have to care (and Toxcct explained that a bit more in details).

              Cédric Moonen Software developer
              Charting control [v1.4] OpenGL game tutorial in C++

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

              But what about GlobalAlloc()? (Sorry, couldn't resist. :) )

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

              Y 1 Reply Last reply
              0
              • J Joe Woodbury

                But what about GlobalAlloc()? (Sorry, couldn't resist. :) )

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

                Y Offline
                Y Offline
                Yajnesh Narayan Behera
                wrote on last edited by
                #7

                There is a verry good discussion here http://www.codeguru.com/forum/showthread.php?t=55854[^]

                J 1 Reply Last reply
                0
                • Y Yajnesh Narayan Behera

                  There is a verry good discussion here http://www.codeguru.com/forum/showthread.php?t=55854[^]

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

                  I was making a joke.

                  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