core dump due to string::c_str ()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • puzzlecracker

    core dump due to string::c_str ()

    Any ideas what may cause that for the string class to core dump?

    #0 0x0018de92 in std::string::c_ str () from /usr/lib/libstdc++.so.5
  • Juha Nieminen

    #2
    Re: core dump due to string::c_str ()

    puzzlecracker wrote:
    Any ideas what may cause that for the string class to core dump?
    >
    #0 0x0018de92 in std::string::c_ str () from /usr/lib/libstdc++.so.5
    Yes: You are accessing out of bounds somewhere.

    Run your program with valgrind.

    Comment

    • red floyd

      #3
      Re: core dump due to string::c_str ()

      On Sep 24, 11:35 am, puzzlecracker <ironsel2...@gm ail.comwrote:
      Any ideas what may cause that for the string class to core dump?
      >
      #0  0x0018de92 in std::string::c_ str () from /usr/lib/libstdc++.so.5
      Come on Puzzlecracker, you should know FAQ 5.8 by now.

      There is an error on line 42 of your code.

      Post a minimal, compileable example that exhibits the behavior in
      question.

      Comment

      • puzzlecracker

        #4
        Re: core dump due to string::c_str ()

        On Sep 24, 3:34 pm, Juha Nieminen <nos...@thanks. invalidwrote:
        puzzlecracker wrote:
        Any ideas what may cause that for the string class to core dump?
        >
        #0  0x0018de92 in std::string::c_ str () from /usr/lib/libstdc++.so.5
        >
          Yes: You are accessing out of bounds somewhere.
        >
          Run your program with valgrind.
        Argh, I traced why it happened, and modified my code, removing the
        stupidity. Here what I had:

        void foo(std::string &str)
        {

        }


        void bar(std::string str)
        {
        foo(str.c_str() );
        }


        Never used valgrind before. How does it work?

        Comment

        • Victor Bazarov

          #5
          Re: core dump due to string::c_str ()

          puzzlecracker wrote:
          On Sep 24, 3:34 pm, Juha Nieminen <nos...@thanks. invalidwrote:
          >puzzlecracke r wrote:
          >>Any ideas what may cause that for the string class to core dump?
          >>#0 0x0018de92 in std::string::c_ str () from /usr/lib/libstdc++.so.5
          > Yes: You are accessing out of bounds somewhere.
          >>
          > Run your program with valgrind.
          >
          Argh, I traced why it happened, and modified my code, removing the
          stupidity. Here what I had:
          >
          void foo(std::string &str)
          {
          >
          }
          >
          >
          void bar(std::string str)
          {
          foo(str.c_str() );
          }
          That shouldn't compile. You're trying to bind a non-const reference to
          a temporary object (created from the pointer to char you get by calling
          the 'c_str()'). That's not allowed. Begin by dialing up the warning
          level on your compiler and possibly by disabling extensions you're not
          consciously using.

          OTOH, there is nothing in this particular code that would cause the
          crash. Are you sure you're not trying to access 'c_str' member using an
          invalid reference?
          Never used valgrind before. How does it work?
          It works quite well.

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask

          Comment

          • puzzlecracker

            #6
            Re: core dump due to string::c_str ()

            On Sep 24, 5:35 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
            puzzlecracker wrote:
            On Sep 24, 3:34 pm, Juha Nieminen <nos...@thanks. invalidwrote:
            puzzlecracker wrote:
            >Any ideas what may cause that for the string class to core dump?
            >#0 0x0018de92 in std::string::c_ str () from /usr/lib/libstdc++.so.5
            Yes: You are accessing out of bounds somewhere.
            >
            Run your program with valgrind.
            >
            Argh, I traced why it happened, and modified my code, removing the
            stupidity. Here what I had:
            >
            void foo(std::string &str)
            {
            >
            }
            >
            void bar(std::string str)
            {
            foo(str.c_str() );
            }
            >
            That shouldn't compile. You're trying to bind a non-const reference to
            a temporary object (created from the pointer to char you get by calling
            the 'c_str()'). That's not allowed. Begin by dialing up the warning
            level on your compiler and possibly by disabling extensions you're not
            consciously using.
            >
            OTOH, there is nothing in this particular code that would cause the
            crash. Are you sure you're not trying to access 'c_str' member using an
            invalid reference?
            >
            Never used valgrind before. How does it work?
            >
            It works quite well.
            >
            V
            --
            Please remove capital 'A's when replying by e-mail
            I do not respond to top-posted replies, please don't ask
            Argh, I was passing string in Foo as a const std::string &

            I thought c_str() returning const char *...Hmm

            Comment

            • James Kanze

              #7
              Re: core dump due to string::c_str ()

              On Sep 25, 3:50 am, puzzlecracker <ironsel2...@gm ail.comwrote:
              On Sep 24, 5:35 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
              puzzlecracker wrote:
              On Sep 24, 3:34 pm, Juha Nieminen <nos...@thanks. invalidwrote:
              >puzzlecracke r wrote:
              >>Any ideas what may cause that for the string class to core dump?
              >>#0 0x0018de92 in std::string::c_ str () from /usr/lib/libstdc++.so.5
              > Yes: You are accessing out of bounds somewhere.
              > Run your program with valgrind.
              Argh, I traced why it happened, and modified my code, removing the
              stupidity. Here what I had:
              void foo(std::string &str)
              {
              }
              void bar(std::string str)
              {
              foo(str.c_str() );
              }
              That shouldn't compile. You're trying to bind a non-const
              reference to a temporary object (created from the pointer to
              char you get by calling the 'c_str()'). That's not allowed.
              Begin by dialing up the warning level on your compiler and
              possibly by disabling extensions you're not consciously
              using.
              OTOH, there is nothing in this particular code that would
              cause the crash. Are you sure you're not trying to access
              'c_str' member using an invalid reference?
              Never used valgrind before. How does it work?
              It works quite well.
              Argh, I was passing string in Foo as a const std::string &
              I thought c_str() returning const char *...Hmm
              It does, but there is an implicit conversion from char const* to
              std::string.

              And as Victor said, there's no reason in the code you posted for
              anything not to work. You're probably overwriting memory
              somewhere else. Valgrind is free; download it and use it.

              --
              James Kanze (GABI Software) email:james.kan ze@gmail.com
              Conseils en informatique orientée objet/
              Beratung in objektorientier ter Datenverarbeitu ng
              9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

              Comment

              • Victor Bazarov

                #8
                Re: core dump due to string::c_str ()

                puzzlecracker wrote:
                On Sep 24, 5:35 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
                >puzzlecracke r wrote:
                >>On Sep 24, 3:34 pm, Juha Nieminen <nos...@thanks. invalidwrote:
                >>>puzzlecracke r wrote:
                >>>>Any ideas what may cause that for the string class to core dump?
                >>>>#0 0x0018de92 in std::string::c_ str () from /usr/lib/libstdc++.so.5
                >>> Yes: You are accessing out of bounds somewhere.
                >>> Run your program with valgrind.
                >>Argh, I traced why it happened, and modified my code, removing the
                >>stupidity. Here what I had:
                >>void foo(std::string &str)
                >>{
                >>}
                >>void bar(std::string str)
                >>{
                >> foo(str.c_str() );
                >>}
                >That shouldn't compile. You're trying to bind a non-const reference to
                >a temporary object (created from the pointer to char you get by calling
                >the 'c_str()'). That's not allowed. Begin by dialing up the warning
                >level on your compiler and possibly by disabling extensions you're not
                >consciously using.
                >>
                >OTOH, there is nothing in this particular code that would cause the
                >crash. Are you sure you're not trying to access 'c_str' member using an
                >invalid reference?
                >>
                >>Never used valgrind before. How does it work?
                >It works quite well.
                >>
                >V
                >--
                >Please remove capital 'A's when replying by e-mail
                >I do not respond to top-posted replies, please don't ask
                >
                Argh, I was passing string in Foo as a const std::string &
                So, is it 'Foo' or 'foo'? Something makes me think that you're not
                posting your real code. See FAQ 5.8.
                >
                I thought c_str() returning const char *...Hmm
                It does.

                V
                --
                Please remove capital 'A's when replying by e-mail
                I do not respond to top-posted replies, please don't ask

                Comment

                • Juha Nieminen

                  #9
                  Re: core dump due to string::c_str ()

                  puzzlecracker wrote:
                  Never used valgrind before. How does it work?
                  You start valgrind giving it your program (and its possible
                  command-line parameters) as parameter (similarly to how you would use
                  eg. the "time" command), and it will report about a wide variety of
                  possible errors in your program.

                  Comment

                  Working...