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. How can I free memory before returning the value in my function?

How can I free memory before returning the value in my function?

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformance
6 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.
  • S Offline
    S Offline
    santiageitorx
    wrote on last edited by
    #1

    How can I free memory before returning the value in my function?. Here's the code, thank you very much. int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); // How can I use 'delete' here before returning New??? return New; } return NULL; }

    M B S 3 Replies Last reply
    0
    • S santiageitorx

      How can I free memory before returning the value in my function?. Here's the code, thank you very much. int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); // How can I use 'delete' here before returning New??? return New; } return NULL; }

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

      Why do you want to delete the array before returning the pointer to the caller? What are you trying to achieve? Mark

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

      1 Reply Last reply
      0
      • S santiageitorx

        How can I free memory before returning the value in my function?. Here's the code, thank you very much. int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); // How can I use 'delete' here before returning New??? return New; } return NULL; }

        B Offline
        B Offline
        Bram van Kampen
        wrote on last edited by
        #3

        You don't delete it before returning the Pointer. The function that called FindString receives this pointer. This calling function can do it's thing with the pointer, and, if needed return it again. When you're done with the value of the pointer you must call delete, otherwise you leak the memory. As soon as you call delete, the value of the pointer points no longer to valid memory, and may not be used again. Using it's value again is an error, and will lead sooner or later to unpredictible behaviour or even crashes. regards

        Bram van Kampen

        1 Reply Last reply
        0
        • S santiageitorx

          How can I free memory before returning the value in my function?. Here's the code, thank you very much. int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); // How can I use 'delete' here before returning New??? return New; } return NULL; }

          S Offline
          S Offline
          santiageitorx
          wrote on last edited by
          #4

          Thanks for your answer. I was wondering if I had to return that pointer how to remove it if it was my desired return value. Thanks for your answer, Bram van Kampen. This way i'd be doing it fine?? int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); memcpy(ReturnArray,s,i); delete[] New; return ReturnArray; } return NULL; }

          N C 2 Replies Last reply
          0
          • S santiageitorx

            Thanks for your answer. I was wondering if I had to return that pointer how to remove it if it was my desired return value. Thanks for your answer, Bram van Kampen. This way i'd be doing it fine?? int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); memcpy(ReturnArray,s,i); delete[] New; return ReturnArray; } return NULL; }

            N Offline
            N Offline
            Nitheesh George
            wrote on last edited by
            #5

            Hi, U declared ReturnArray as a local buffer. ie it's scope is only within the function rr. So after the execution of rr ,ReturnArray will be destroyed. i think u r going to get a "memory cannot be read error" . am i right?. thanks Nitheesh

            1 Reply Last reply
            0
            • S santiageitorx

              Thanks for your answer. I was wondering if I had to return that pointer how to remove it if it was my desired return value. Thanks for your answer, Bram van Kampen. This way i'd be doing it fine?? int FindString(char*s, char* what, int len, int len2) { char * x = s; for (int i = 0;i 0) { char * New = new char[i]; memcpy(New,s,i); memcpy(ReturnArray,s,i); delete[] New; return ReturnArray; } return NULL; }

              C Offline
              C Offline
              Chiew Heng Wah
              wrote on last edited by
              #6

              You could declare it as a global variable, outside the function instead: char ReturnArray[1024] = {0}; // put it here. char *rr(char*s) { char cc[4] = {0x00,0x00,0x00,0x00}; // char ReturnArray[1024] = {0}; // move it up instead.

              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