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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString::Empty()

CString::Empty()

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformanceannouncement
4 Posts 4 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
    gReaen
    wrote on last edited by
    #1

    I need to write my version of code similar to CString::Empty() function for char*. Is it enough if i just free the memory of the string to implement the function. How do i empty the string otherwise? Thanks in advance

    B J CPalliniC 3 Replies Last reply
    0
    • G gReaen

      I need to write my version of code similar to CString::Empty() function for char*. Is it enough if i just free the memory of the string to implement the function. How do i empty the string otherwise? Thanks in advance

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

      If you are talking C or working directly with C style string in C++, then you do not need a function.

      char* pData = (char*)malloc(size of data required); // C-code
      if( pData ) // Same as: if( pData != NULL )
      // Do something with data block
      free(pData); // Release memory
      pData = NULL; // Only needed if some other piece of code needs to use it
      // Some where else in you code
      if( pData )
      // Do something with the data block
      else
      // Invalid pointer

      You do not really need a function, unless you are designing your own sting class. Good Luck!

      INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

      1 Reply Last reply
      0
      • G gReaen

        I need to write my version of code similar to CString::Empty() function for char*. Is it enough if i just free the memory of the string to implement the function. How do i empty the string otherwise? Thanks in advance

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

        gReaen wrote:

        Is it enough if i just free the memory of the string to implement the function. How do i empty the string otherwise?

        Yes and No! After you free it you must NEVER again refer to that section of memory. The way I usually do it is: char* p=(char*)malloc(size);...... free(p); p=NULL; At least you get a definite runtime error if you try to do something with p after freeing, instead of an obscure runtime bug.

        Bram van Kampen

        1 Reply Last reply
        0
        • G gReaen

          I need to write my version of code similar to CString::Empty() function for char*. Is it enough if i just free the memory of the string to implement the function. How do i empty the string otherwise? Thanks in advance

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Actually if you designed your string as a class then you don't even need to free memory, you can just mark the string as empty: this way avoiding memory deallocation/rellaocation if there is chance the string will be used again. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          [my articles]

          In testa che avete, signor di Ceprano?

          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