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. Managed C++/CLI
  4. Please help memory management

Please help memory management

Scheduled Pinned Locked Moved Managed C++/CLI
performancehelpquestion
3 Posts 2 Posters 4 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.
  • U Offline
    U Offline
    User 12795237
    wrote on last edited by
    #1

    void CreateFolderPath(string Path) { char *strFolderPath = new char[Path.size() + 1]; strcpy_s(strFolderPath, Path.size() + 1, Path.c_str()); _mkdir(strFolderPath); } string ConvertIntToString(int cipher, int objintiger) { string temp; char *Res = new char[cipher]; sprintf_s(Res, cipher, "%02d", objintiger); temp = Res; return temp; } // Is there room for memory problems?

    J 1 Reply Last reply
    0
    • U User 12795237

      void CreateFolderPath(string Path) { char *strFolderPath = new char[Path.size() + 1]; strcpy_s(strFolderPath, Path.size() + 1, Path.c_str()); _mkdir(strFolderPath); } string ConvertIntToString(int cipher, int objintiger) { string temp; char *Res = new char[cipher]; sprintf_s(Res, cipher, "%02d", objintiger); temp = Res; return temp; } // Is there room for memory problems?

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      There are problems (memory leaks). You must delete memory allocated with new when not used anymore:

      void CreateFolderPath(string Path)
      {
      char *strFolderPath = new char[Path.size() + 1];
      strcpy_s(strFolderPath, Path.size() + 1, Path.c_str());
      _mkdir(strFolderPath);
      delete [] strFolderPath;
      }

      string ConvertIntToString(int cipher, int objintiger)
      {
      string temp;
      char *Res = new char[cipher];
      sprintf_s(Res, cipher, "%02d", objintiger);
      temp = Res;
      delete [] Res;
      return temp;
      }

      For your first example there is even no need to copy the string. Just pass it to _mkdir():

      void CreateFolderPath(string Path)
      {
      _mkdir(Path.c_str());
      }

      U 1 Reply Last reply
      0
      • J Jochen Arndt

        There are problems (memory leaks). You must delete memory allocated with new when not used anymore:

        void CreateFolderPath(string Path)
        {
        char *strFolderPath = new char[Path.size() + 1];
        strcpy_s(strFolderPath, Path.size() + 1, Path.c_str());
        _mkdir(strFolderPath);
        delete [] strFolderPath;
        }

        string ConvertIntToString(int cipher, int objintiger)
        {
        string temp;
        char *Res = new char[cipher];
        sprintf_s(Res, cipher, "%02d", objintiger);
        temp = Res;
        delete [] Res;
        return temp;
        }

        For your first example there is even no need to copy the string. Just pass it to _mkdir():

        void CreateFolderPath(string Path)
        {
        _mkdir(Path.c_str());
        }

        U Offline
        U Offline
        User 12795237
        wrote on last edited by
        #3

        Thank you very much!!!! :)

        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