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. Avoiding a memory leak

Avoiding a memory leak

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
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.
  • G Offline
    G Offline
    georgiek50
    wrote on last edited by
    #1

    I have a function that returns a char* and would like to know which way to handle this as to avoid a leak. If the function were in this format char* Function(void) { char *szString = new char[50]; return szString; } And was called like so: char *szBuffer = Function(); would deleting szBuffer like so: delete szBuffer effectively erase the allocated memory?

    A P 2 Replies Last reply
    0
    • G georgiek50

      I have a function that returns a char* and would like to know which way to handle this as to avoid a leak. If the function were in this format char* Function(void) { char *szString = new char[50]; return szString; } And was called like so: char *szBuffer = Function(); would deleting szBuffer like so: delete szBuffer effectively erase the allocated memory?

      A Offline
      A Offline
      Anthony_Yio
      wrote on last edited by
      #2

      You need to call delete this way delete [] szBuffer; Sonork 100.41263:Anthony_Yio

      Y G 2 Replies Last reply
      0
      • A Anthony_Yio

        You need to call delete this way delete [] szBuffer; Sonork 100.41263:Anthony_Yio

        Y Offline
        Y Offline
        Yulianto
        wrote on last edited by
        #3

        I can do it too with delete(szBuffer), right? :)

        1 Reply Last reply
        0
        • A Anthony_Yio

          You need to call delete this way delete [] szBuffer; Sonork 100.41263:Anthony_Yio

          G Offline
          G Offline
          georgiek50
          wrote on last edited by
          #4

          Thanks for the reply but a subquestion. Aren't the brackets supposed to be placed after delete only in case of multidimensional array? and if not, does this mean I have memory leaks all over my code when I have used new and delete directly? (...oh god no!!!) eg. char *szString = new char[20]; delete szString Or does this apply solely because it was returned from a function?

          G A 2 Replies Last reply
          0
          • G georgiek50

            Thanks for the reply but a subquestion. Aren't the brackets supposed to be placed after delete only in case of multidimensional array? and if not, does this mean I have memory leaks all over my code when I have used new and delete directly? (...oh god no!!!) eg. char *szString = new char[20]; delete szString Or does this apply solely because it was returned from a function?

            G Offline
            G Offline
            georgiek50
            wrote on last edited by
            #5

            hmmm, I just realized that ommitting the brackets only delets the first element...I have a lot of search/replacing to do!!!! Thanks for the help

            J 1 Reply Last reply
            0
            • G georgiek50

              Thanks for the reply but a subquestion. Aren't the brackets supposed to be placed after delete only in case of multidimensional array? and if not, does this mean I have memory leaks all over my code when I have used new and delete directly? (...oh god no!!!) eg. char *szString = new char[20]; delete szString Or does this apply solely because it was returned from a function?

              A Offline
              A Offline
              Anthony_Yio
              wrote on last edited by
              #6

              For multidimensional array like below int *pSomething = new int [2][2]; the clean up codes will be delete [] [] pSomething; I am not sure if char *szString = new char[20]; delete szString \ [MODIFIED] will cause a memory leak to your program [/MODIFIED] Sonork 100.41263:Anthony_Yio

              1 Reply Last reply
              0
              • G georgiek50

                hmmm, I just realized that ommitting the brackets only delets the first element...I have a lot of search/replacing to do!!!! Thanks for the help

                J Offline
                J Offline
                John R Shaw
                wrote on last edited by
                #7

                Actualy ommitting the brackets does free the memory, but only calls that destructor for the first object in the array. The brackets tells delete to call the destructor of every item in the array. Therefor, for basic types you could get away with not using the brackets (not recommened). If you are creating an array of objects that also allocate memory for there own use, then you must use the brackets so that there destructors will be called inorder for them to clean up after themselfs (a.k.a. free any memory they allocated). INTP

                1 Reply Last reply
                0
                • G georgiek50

                  I have a function that returns a char* and would like to know which way to handle this as to avoid a leak. If the function were in this format char* Function(void) { char *szString = new char[50]; return szString; } And was called like so: char *szBuffer = Function(); would deleting szBuffer like so: delete szBuffer effectively erase the allocated memory?

                  P Offline
                  P Offline
                  palbano
                  wrote on last edited by
                  #8

                  Marshall Cline's FAQ is a great resource for looking things up C++ C++ FAQ

                  "No matter where you go, there your are." - Buckaroo Banzai

                  -pete

                  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