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 GetBuffer

CString GetBuffer

Scheduled Pinned Locked Moved C / C++ / MFC
cssquestion
3 Posts 2 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.
  • N Offline
    N Offline
    nss
    wrote on last edited by
    #1

    I'm reading MSDN but am not really sure whats happening:

        dbName = "";
        size=128;
    LPSTR psz = dbName.GetBuffer(size);
    

    SO does this mean that I've fixed things so that I can copy somethig less than 128 bytes into dbName? SO if I just did dbName = "aaaaaaaaaaaaaaaaa" directly who takes care of the fact that dbName was zero bytes long ("" emoty) before? When is one method better than another? Or --- when it is necessary to do the GetBuffer strategy?

    M 1 Reply Last reply
    0
    • N nss

      I'm reading MSDN but am not really sure whats happening:

          dbName = "";
          size=128;
      LPSTR psz = dbName.GetBuffer(size);
      

      SO does this mean that I've fixed things so that I can copy somethig less than 128 bytes into dbName? SO if I just did dbName = "aaaaaaaaaaaaaaaaa" directly who takes care of the fact that dbName was zero bytes long ("" emoty) before? When is one method better than another? Or --- when it is necessary to do the GetBuffer strategy?

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Check out my C++ string classes[^] article where I cover CString in detail. But to summarize, dbName.GetBuffer(128) makes sure that the dbName buffer is at least 128 characters long (allocating more space for you, if necessary) and returns a non-const pointer to the buffer. You can then use that pointer as if it were a normal C-style string. When you call any other CString methods (such as the = operator), that pointer may become invalid because some CString methods delete/reallocate the buffer. nss wrote: SO if I just did dbName = "aaaaaaaaaaaaaaaaa" directly who takes care of the fact that dbName was zero bytes long before? CString manages its own memory (except for when you explicitly take control of the memory with GetBuffer()). --Mike-- I'm bored... Episode I bored. 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm

      N 1 Reply Last reply
      0
      • M Michael Dunn

        Check out my C++ string classes[^] article where I cover CString in detail. But to summarize, dbName.GetBuffer(128) makes sure that the dbName buffer is at least 128 characters long (allocating more space for you, if necessary) and returns a non-const pointer to the buffer. You can then use that pointer as if it were a normal C-style string. When you call any other CString methods (such as the = operator), that pointer may become invalid because some CString methods delete/reallocate the buffer. nss wrote: SO if I just did dbName = "aaaaaaaaaaaaaaaaa" directly who takes care of the fact that dbName was zero bytes long before? CString manages its own memory (except for when you explicitly take control of the memory with GetBuffer()). --Mike-- I'm bored... Episode I bored. 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm

        N Offline
        N Offline
        nss
        wrote on last edited by
        #3

        Will do. Many thanks! MFC is always surprising me....:)

        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