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. standard c_str() problem in C++

standard c_str() problem in C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
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.
  • A Offline
    A Offline
    Anitesh Kumar
    wrote on last edited by
    #1

    I have craeted following kind of function in a class: void SetStr(string& s1, string& s2) { const char* s =(s1+s2).c_str(); cout<

    E 1 Reply Last reply
    0
    • A Anitesh Kumar

      I have craeted following kind of function in a class: void SetStr(string& s1, string& s2) { const char* s =(s1+s2).c_str(); cout<

      E Offline
      E Offline
      Emilio Garavaglia
      wrote on last edited by
      #2

      The result of an expression is temporary, and it is destroyed an the and of the expression evaluation. In your first case, you are getting a temporary string as a result of s1+s2, whose buffer pointer is saved in s. After that, the expression finish, the temporary is destroyed, and so it is its buffer and s is left dangling. You had been lucky in having printed nothing. accessing a deleted buffer can even result in a crash. In your second example, the temporary string resulting from s1+s2 is kept alive until the expression it belongs ( cout<<(s1+s2).c_str()< ) is evaluated. Hence its buffer (renturned from `c_str()`) is still there at the time its pointer is given to `cout`. Your first sample works correctly if you retain the string: void SetStr(string& s1, string& s2) { string ss; //the place to store the result ss = s1+s2; //the result of s1+s2 is moved to ss, that survives the expression itself const char* s = ss.c_str(); //ss buffer pointer obtained cout << s << endl; //your original cout } 2 bugs found. > recompile ... 65534 bugs found. :doh:

      CPalliniC 1 Reply Last reply
      0
      • E Emilio Garavaglia

        The result of an expression is temporary, and it is destroyed an the and of the expression evaluation. In your first case, you are getting a temporary string as a result of s1+s2, whose buffer pointer is saved in s. After that, the expression finish, the temporary is destroyed, and so it is its buffer and s is left dangling. You had been lucky in having printed nothing. accessing a deleted buffer can even result in a crash. In your second example, the temporary string resulting from s1+s2 is kept alive until the expression it belongs ( cout<<(s1+s2).c_str()< ) is evaluated. Hence its buffer (renturned from `c_str()`) is still there at the time its pointer is given to `cout`. Your first sample works correctly if you retain the string: void SetStr(string& s1, string& s2) { string ss; //the place to store the result ss = s1+s2; //the result of s1+s2 is moved to ss, that survives the expression itself const char* s = ss.c_str(); //ss buffer pointer obtained cout << s << endl; //your original cout } 2 bugs found. > recompile ... 65534 bugs found. :doh:

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

        Great answer. You miss an underscore in your sample code. --Carlo The Nitpick

        Veni, vidi, vici.

        In testa che avete, signor di Ceprano?

        E 1 Reply Last reply
        0
        • CPalliniC CPallini

          Great answer. You miss an underscore in your sample code. --Carlo The Nitpick

          Veni, vidi, vici.

          E Offline
          E Offline
          Eytukan
          wrote on last edited by
          #4

          But no applause for this guy standard c_str() problem in C++[^] :sigh: :-D but why do we have two forums on the same subject. Sorry I'm just outta the rock.

          Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.

          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