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. Apparently different behaviour assigning C string to std::string, for Relese/Debug

Apparently different behaviour assigning C string to std::string, for Relese/Debug

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

    Hi, I think I'm getting different behaviours with the code below for Release and Debug compiles.

    FILE* in ;
    err = fopen_s (&in, fileName.c_str(), "r");
    if (!in) {
    std::cerr << "cannot open input file '" << fileName << "'" << std::endl;
    return;
    }
    char buff[1024];
    while (fgets(buff, 1024, in)) {

    std::string line=buff;
    

    //...

    line is getting the characters ok when compiled in Debug. In Release though, line is BadPtr. I guess I'm doing the initialization wrong, but I don't see why.

    J L _ 3 Replies Last reply
    0
    • P piul

      Hi, I think I'm getting different behaviours with the code below for Release and Debug compiles.

      FILE* in ;
      err = fopen_s (&in, fileName.c_str(), "r");
      if (!in) {
      std::cerr << "cannot open input file '" << fileName << "'" << std::endl;
      return;
      }
      char buff[1024];
      while (fgets(buff, 1024, in)) {

      std::string line=buff;
      

      //...

      line is getting the characters ok when compiled in Debug. In Release though, line is BadPtr. I guess I'm doing the initialization wrong, but I don't see why.

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      Perhaps this[^] link will help.

      "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

      P 1 Reply Last reply
      0
      • P piul

        Hi, I think I'm getting different behaviours with the code below for Release and Debug compiles.

        FILE* in ;
        err = fopen_s (&in, fileName.c_str(), "r");
        if (!in) {
        std::cerr << "cannot open input file '" << fileName << "'" << std::endl;
        return;
        }
        char buff[1024];
        while (fgets(buff, 1024, in)) {

        std::string line=buff;
        

        //...

        line is getting the characters ok when compiled in Debug. In Release though, line is BadPtr. I guess I'm doing the initialization wrong, but I don't see why.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I suspect there is something you are not showing us. I have just tried your code and it works in both Debug and Release modes.

        1 Reply Last reply
        0
        • P piul

          Hi, I think I'm getting different behaviours with the code below for Release and Debug compiles.

          FILE* in ;
          err = fopen_s (&in, fileName.c_str(), "r");
          if (!in) {
          std::cerr << "cannot open input file '" << fileName << "'" << std::endl;
          return;
          }
          char buff[1024];
          while (fgets(buff, 1024, in)) {

          std::string line=buff;
          

          //...

          line is getting the characters ok when compiled in Debug. In Release though, line is BadPtr. I guess I'm doing the initialization wrong, but I don't see why.

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          It's possibly a configuration issue. Meaning you've some configuration for Debug that you haven't done for Release.

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++) (October 2009 - September 2013)

          Polymorphism in C

          P 1 Reply Last reply
          0
          • J jeron1

            Perhaps this[^] link will help.

            "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

            P Offline
            P Offline
            piul
            wrote on last edited by
            #5

            My excuses for not answering during the weekend. As suggested by some comment on the link you posted (thank you) the problem seems to be in the fgets function. It is storing senseless data in buff for Release, and then the std::string constructor can't make sense of it. I will try with std::getline().

            1 Reply Last reply
            0
            • _ _Superman_

              It's possibly a configuration issue. Meaning you've some configuration for Debug that you haven't done for Release.

              «_Superman_»  _I love work. It gives me something to do between weekends.

              _Microsoft MVP (Visual C++) (October 2009 - September 2013)

              Polymorphism in C

              P Offline
              P Offline
              piul
              wrote on last edited by
              #6

              It is, quite probably. fgets (not std::string constructor as I initially posted) is behaving different if I set different options for Optimization and Runtime Library in MSVS2005. I can't make Release work like Debug though, even setting the same options.

              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