Pointer string assignment problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shan_rish@yahoo.com

    #1

    Pointer string assignment problem

    Hi Group,
    When i compile the following program it compiles successfully, but
    crashes while executing. I am trying to assign a NULL char pointer to a
    string. The error message is

    /home1/murugan/prog/ccprog >./a.out
    Abort(coredump)
    /home1/murugan/prog/ccprog >


    The program is :

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
    string request;
    char* str = 0;

    request = str;
    cout<<endl<<req uest<<endl;
    return 1;


    }

    Thanks in advance.

    Cheers
    Shan

  • Alf P. Steinbach

    #2
    Re: Pointer string assignment problem

    * shan_rish@yahoo .com:[color=blue]
    >
    > When i compile the following program it compiles successfully, but
    > crashes while executing. I am trying to assign a NULL char pointer to a
    > string.[/color]

    You can't, or at least, shouldn't.

    std::string has no representation of null-strings.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Zara

      #3
      Re: Pointer string assignment problem

      On 1 Dec 2005 21:38:23 -0800, shan_rish@yahoo .com wrote:
      [color=blue]
      >Hi Group,
      >When i compile the following program it compiles successfully, but
      >crashes while executing. I am trying to assign a NULL char pointer to a
      >string. The error message is
      >
      >/home1/murugan/prog/ccprog >./a.out
      >Abort(coredump )
      >/home1/murugan/prog/ccprog >
      >
      >
      >The program is :
      >
      >#include <iostream>
      >#include <string>
      >
      >using namespace std;
      >
      >int main()
      >{
      > string request;
      > char* str = 0;
      >
      > request = str;
      > cout<<endl<<req uest<<endl;
      > return 1;
      >
      >
      >}
      >
      >Thanks in advance.
      >
      >Cheers
      >Shan[/color]


      You may assign a char pointer to a string provided a) it is not a null
      pointer and b) it points to a C string (that is, terminated with a
      '\0'.

      If your aim is to assgin an empty string, there are various ways, two
      of them are: request.clear() and request="",

      Regards,
      -- Zara

      Comment

      • shan_rish@yahoo.com

        #4
        Re: Pointer string assignment problem

        Zara wrote:[color=blue]
        > On 1 Dec 2005 21:38:23 -0800, shan_rish@yahoo .com wrote:
        >
        > You may assign a char pointer to a string provided a) it is not a null
        > pointer and b) it points to a C string (that is, terminated with a
        > '\0'.
        >
        > If your aim is to assgin an empty string, there are various ways, two
        > of them are: request.clear() and request="",
        >
        > Regards,
        > -- Zara[/color]

        Thank you guys. It was an old code and we are trying to find out an
        work around. The code i sent to the group simulates the problem which
        we are facing in the real code.

        Cheers
        Shan.

        Comment

        • Niklas Norrthon

          #5
          Re: Pointer string assignment problem

          shan_rish@yahoo .com writes:
          [color=blue]
          > Hi Group,
          > When i compile the following program it compiles successfully, but
          > crashes while executing.[/color]

          Be happy. Undefined behavior can be much worse than that, for
          example it could crash every time somebody else runs the program,
          but never when you run it. (Such behavior isn't unheard of, and
          can be pretty tricky to track down).
          [color=blue]
          >I am trying to assign a NULL char pointer to a
          > string.[/color]

          I really don't understand why you post here when you know what you
          do wrong.

          The error message is
          [color=blue]
          > /home1/murugan/prog/ccprog >./a.out
          > Abort(coredump)
          > /home1/murugan/prog/ccprog >[/color]

          Looks like a unix like system... If you are lucky there might be
          a file named core, a.out.core or something similar that you could
          use to debug your program.
          [color=blue]
          > The program is :
          >
          > #include <iostream>
          > #include <string>
          >
          > using namespace std;
          >
          > int main()
          > {
          > string request;[/color]

          This line creates an empty string with automatic storage.
          [color=blue]
          > char* str = 0;[/color]

          This line creates a pointer to char, initializing it to NULL, that
          is it doesn't point anywhere.
          [color=blue]
          > request = str;[/color]

          This line calls the version of the assignment operator of std::string
          that takes a const char* argument, which creates a new string object,
          by calling the version of the constructor that takes a const char*
          argument, and then assigns that new string to request.

          In the constructor string::string( const char* s) the argument 's'
          must be a valid pointer to a memory region containing a zero-
          terminated string. NULL is not such a value, so you have
          undefined behavior, and from here anything could happen, Crashing
          immediatly is most common on modern platforms where NULL points
          to unmapped memory, but be careful, you can't count on that...
          [color=blue]
          > cout<<endl<<req uest<<endl;[/color]

          If you had removed NULL assignment above, this line would print
          two newlines, since request is empty after it's default constructor.

          I find '\n' clearer than std::endl, so personally I would have said:
          cout << '\n' << request << '\n'; But that is mostly a matter of taste,
          there are some subtle differences between the two versions, but they
          rarely matter.
          [color=blue]
          > return 1;[/color]

          Portable return values from main are 0, EXIT_SUCCESS and EXIT_FAILURE,
          where the latter two are defined in <cstdlib>, and the former means the
          same as EXIT_SUCCESS. Other values are possible, but not portable.
          <OT>
          On unix systems the return value of 1 typically means failure of some
          sort.
          </OT>

          To summarize: Get rid of the char*, and things should work.

          /Niklas Norrthon


          Comment

          • Marcus Kwok

            #6
            Re: Pointer string assignment problem

            Niklas Norrthon <do-not-use@invalid.net > wrote:[color=blue]
            > shan_rish@yahoo .com writes:[color=green]
            >> cout<<endl<<req uest<<endl;[/color]
            >
            > I find '\n' clearer than std::endl, so personally I would have said:
            > cout << '\n' << request << '\n'; But that is mostly a matter of taste,
            > there are some subtle differences between the two versions, but they
            > rarely matter.[/color]

            I think the most significant difference is that

            std::cout << std::endl;

            is basically equivalent to

            std::cout << '\n' << std::flush;

            which _might_ affect performance. If you know you _need_ to flush the
            stream, std::endl should be fine. Personally, I usually use '\n' also,
            except in my logging class.

            --
            Marcus Kwok

            Comment

            Working...