strcat

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

    strcat

    why the following code will crash?

    int main()
    {
    char* str = "aa";
    strcat (str, "hello");
    cout << str << "\n";
    strcat (str, "you1");
    cout << str << "\n";

    return 0;
    }
  • Ian Collins

    #2
    Re: strcat

    Eric Kaplan wrote:
    why the following code will crash?
    >
    Do you mean after it has been fixed to compile?
    int main()
    {
    char* str = "aa";
    strcat (str, "hello");
    What is this trying to do? (hint what is str?).

    --
    Ian Collins.

    Comment

    • Sharad

      #3
      Re: strcat


      "Eric Kaplan" <tobycraftse@ya hoo.comwrote in message
      news:u048v3tmo9 qgkkh37qs8ptek9 ebd93jr5t@4ax.c om...
      why the following code will crash?
      >
      int main()
      {
      char* str = "aa";
      Where is str pointing to? It's pointing to a string literal.
      strcat (str, "hello");
      Is the location pointed to by str modifiable?



      Comment

      • Sharad

        #4
        Re: strcat


        <acehreli@gmail .comwrote in message
        On Apr 2, 4:01 pm, Eric Kaplan <tobycraf...@ya hoo.comwrote:
        >why the following code will crash?
        >>
        >int main()
        >{
        > char* str = "aa";
        > strcat (str, "hello");
        > cout << str << "\n";
        > strcat (str, "you1");
        > cout << str << "\n";
        >>
        > return 0;
        >>
        >}
        >
        <snip>
        2) String literals are constant objects, which means that the
        characters that they contain may not be modified. The fact that it is
        IIRC, this statement is not true. String literals as defined in the C
        Standard are not defined as constant. Yet, modifying them leads to undefined
        behavior. This is what I recollect from C89 (I have to admit that it's been
        quite some time, so I may be wrong). Most of the Unix compilers place string
        literals in a read only region, so modifying them gets a segmention
        violation there.

        Sharad


        Comment

        • Ron Natalie

          #5
          Re: strcat

          Eric Kaplan wrote:
          why the following code will crash?
          >
          int main()
          {
          char* str = "aa";
          This is a deprecated conversion from a 3 element array of const char
          (holding the values a a and \0) to pointer to (single) character.
          strcat (str, "hello");
          This attempts to write hello on to the end of the chars starting
          at str. The h is written into read only storage, the ello\0 is
          written outside of any allocated memory.
          cout << str << "\n";
          strcat (str, "you1");
          cout << str << "\n";
          \
          }
          char* is not a string type.
          strcat doesn't allocate any memory. You are expected to have enough
          allocated memory in the bytes pointed to by the first argument to
          hold the concatenation of both strings.

          This is C++ and you shouldn't be playing with C's old and busted
          string functions but the C++ new hotness string type.

          std::string str = "aa";
          str += "hello";
          cout << str << "\n"

          see.

          Comment

          • utab

            #6
            Re: strcat

            You should not take advantage of that in new code. So instead of
            writing
            >
            char* str = "aa";
            >
            write
            >
            const char* str = "aa";
            >
            Ali
            For the code at hand, this does not help to recover the segfault
            though.

            Still you will be trying to change a const char array. So why bother,
            use strings without the implementation details of pointers ;),
            pointers/arrays are evil :-), as far as possible I refrain to use
            them.

            Comment

            • Sharad

              #7
              Re: strcat


              "utab" <umut.tabak@gma il.comwrote in message
              news:e8a61d54-c9d0-4c4e-9ba5-65c23159ced5@e6 g2000prf.google groups.com...
              >You should not take advantage of that in new code. So instead of
              >writing
              >>
              > char* str = "aa";
              >>
              >write
              >>
              > const char* str = "aa";
              >>
              >Ali
              >
              For the code at hand, this does not help to recover the segfault
              though.
              It does in some way. The program won't compile now.
              Still you will be trying to change a const char array. So why bother,
              use strings without the implementation details of pointers ;),
              pointers/arrays are evil :-), as far as possible I refrain to use
              them.
              Yes, in C++ you are better off with std::string and std::vector.

              Sharad


              Comment

              • utab

                #8
                Re: strcat

                For the code at hand, this does not help to recover the segfault
                though.
                >
                It does in some way. The program won't compile now.
                Ah yes, using "const" becomes an insurance in this case. That is good
                practice.
                >
                Still you will be trying to change a const char array. So why bother,
                use strings without the implementation details of pointers ;),
                pointers/arrays are evil :-), as far as possible I refrain to use
                them.
                >
                Yes, in C++ you are better off with std::string and std::vector.
                >
                Sharad

                Comment

                • acehreli@gmail.com

                  #9
                  Re: strcat

                  On Apr 2, 4:41 pm, "Sharad" <sharadk.nospam _...@yahoo.comw rote:
                  <acehr...@gmail .comwrote in message
                  On Apr 2, 4:01 pm, Eric Kaplan <tobycraf...@ya hoo.comwrote:
                  why the following code will crash?
                  >
                  int main()
                  {
                  char* str = "aa";
                  strcat (str, "hello");
                  cout << str << "\n";
                  strcat (str, "you1");
                  cout << str << "\n";
                  >
                  return 0;
                  >
                  }
                  >
                  <snip>
                  2) String literals are constant objects, which means that the
                  characters that they contain may not be modified. The fact that it is
                  >
                  IIRC, this statement is not true. String literals as defined in the C
                  Standard are not defined as constant. Yet, modifying them leads to undefined
                  behavior.
                  So they are constant.
                  This is what I recollect from C89 (I have to admit that it's been
                  quite some time, so I may be wrong).
                  Same here, but luckily we are on a C++ forum and discussing a C++
                  code. :)
                  Most of the Unix compilers place string
                  literals in a read only region, so modifying them gets a segmention
                  violation there.
                  That's because string literals are constant. :)

                  Ali

                  Comment

                  • Sharad

                    #10
                    Re: strcat


                    <acehreli@gmail .comwrote in message
                    On Apr 2, 4:41 pm, "Sharad" <sharadk.nospam _...@yahoo.comw rote:
                    ><acehr...@gmai l.comwrote in message
                    On Apr 2, 4:01 pm, Eric Kaplan <tobycraf...@ya hoo.comwrote:
                    >why the following code will crash?
                    >>
                    >int main()
                    >{
                    > char* str = "aa";
                    > strcat (str, "hello");
                    > cout << str << "\n";
                    > strcat (str, "you1");
                    > cout << str << "\n";
                    >>
                    > return 0;
                    >>
                    >}
                    >>
                    ><snip>
                    2) String literals are constant objects, which means that the
                    characters that they contain may not be modified. The fact that it is
                    >>
                    >IIRC, this statement is not true. String literals as defined in the C
                    >Standard are not defined as constant. Yet, modifying them leads to
                    >undefined
                    >behavior.
                    >
                    So they are constant.
                    I don't think so. I will need to go back to the C Standard to verify that.
                    >This is what I recollect from C89 (I have to admit that it's been
                    >quite some time, so I may be wrong).
                    >
                    Same here, but luckily we are on a C++ forum and discussing a C++
                    code. :)
                    Nah, C++ has inherited a lot from C. Most C programs are valid C++ programs
                    also. And the behavior we are talking about is the inherited behavior.
                    >Most of the Unix compilers place string
                    >literals in a read only region, so modifying them gets a segmention
                    >violation there.
                    >
                    That's because string literals are constant. :)
                    Read above.

                    Sharad


                    Comment

                    • Ian Collins

                      #11
                      Re: strcat

                      Sharad wrote:
                      <acehreli@gmail .comwrote:
                      >So they are constant.
                      >
                      I don't think so. I will need to go back to the C Standard to verify that.
                      >
                      2.13.4 of the C++ standard states that a string literal has type "array
                      of n const char".

                      --
                      Ian Collins.

                      Comment

                      • Sharad

                        #12
                        Re: strcat


                        "Ian Collins" <ian-news@hotmail.co mwrote in message
                        news:65inveF2g3 gvtU4@mid.indiv idual.net...
                        Sharad wrote:
                        ><acehreli@gmai l.comwrote:
                        >
                        >>So they are constant.
                        >>
                        >I don't think so. I will need to go back to the C Standard to verify
                        >that.
                        >>
                        2.13.4 of the C++ standard states that a string literal has type "array
                        of n const char".
                        OK thanks!


                        Comment

                        • Yannick Tremblay

                          #13
                          Re: strcat

                          In article <e8a61d54-c9d0-4c4e-9ba5-65c23159ced5@e6 g2000prf.google groups.com>,
                          utab <umut.tabak@gma il.comwrote:
                          >You should not take advantage of that in new code. So instead of
                          >writing
                          >>
                          > char* str = "aa";
                          >>
                          >write
                          >>
                          > const char* str = "aa";
                          >>
                          >Ali
                          >
                          >For the code at hand, this does not help to recover the segfault
                          >though.
                          There won't be any segfault because the code won't compile.





                          Comment

                          Working...