std::reverse_copy() woes

Discussion in 'C++' started by Eric Lilja, Jan 17, 2007.

  1. Eric Lilja

    Eric Lilja Guest

    Hello, consider the following program:

    #include <algorithm>
    #include <cstring>
    #include <iostream>
    #include <iterator>
    #include <string>

    int main()
    {
    const char *foo = "hello";
    std::string s;

    /* Prints olleh */
    std::reverse_copy(foo, foo + std::strlen(foo),
    std::eek:stream_iterator<char>(std::cout));

    std::reverse_copy(foo, foo + std::strlen(foo), s.begin());

    /* Prints nothing, why? */
    std::cout << std::endl << s << std::endl;
    }

    The second call to reverse_copy doesn't work as expected because when I
    print s I get nothing. Using a debugger, I inspect s just before and
    right after the call to reverse_copy:
    (gdb) p s
    $1 = {static npos = 4294967295, _M_dataplus = {<allocator<char>> =
    {<new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p =
    0x44212c ""}}
    (gdb) n
    (gdb) p s
    $2 = {static npos = 4294967295, _M_dataplus = {<allocator<char>> =
    {<new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p =
    0x71254c "olleh"}}
    (gdb)

    So it seems to contain the reversed string, but it won't output?!
    What's going on?

    - Eric
    Eric Lilja, Jan 17, 2007
    #1
    1. Advertising

  2. Eric Lilja

    Ondra Holub Guest

    Eric Lilja napsal:
    > Hello, consider the following program:
    >
    > #include <algorithm>
    > #include <cstring>
    > #include <iostream>
    > #include <iterator>
    > #include <string>
    >
    > int main()
    > {
    > const char *foo = "hello";
    > std::string s;
    >
    > /* Prints olleh */
    > std::reverse_copy(foo, foo + std::strlen(foo),
    > std::eek:stream_iterator<char>(std::cout));
    >
    > std::reverse_copy(foo, foo + std::strlen(foo), s.begin());
    >
    > /* Prints nothing, why? */
    > std::cout << std::endl << s << std::endl;
    > }
    >
    > The second call to reverse_copy doesn't work as expected because when I
    > print s I get nothing. Using a debugger, I inspect s just before and
    > right after the call to reverse_copy:
    > (gdb) p s
    > $1 = {static npos = 4294967295, _M_dataplus = {<allocator<char>> =
    > {<new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p =
    > 0x44212c ""}}
    > (gdb) n
    > (gdb) p s
    > $2 = {static npos = 4294967295, _M_dataplus = {<allocator<char>> =
    > {<new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p =
    > 0x71254c "olleh"}}
    > (gdb)
    >
    > So it seems to contain the reversed string, but it won't output?!
    > What's going on?
    >
    > - Eric


    Copy it to back_inserter iterator:

    std::reverse_copy(foo, foo + std::strlen(foo),
    std::back_inserter(s));
    Ondra Holub, Jan 17, 2007
    #2
    1. Advertising

  3. Eric Lilja

    Ondra Holub Guest

    Eric Lilja napsal:
    > Hello, consider the following program:
    >
    > #include <algorithm>
    > #include <cstring>
    > #include <iostream>
    > #include <iterator>
    > #include <string>
    >
    > int main()
    > {
    > const char *foo = "hello";
    > std::string s;
    >
    > /* Prints olleh */
    > std::reverse_copy(foo, foo + std::strlen(foo),
    > std::eek:stream_iterator<char>(std::cout));
    >
    > std::reverse_copy(foo, foo + std::strlen(foo), s.begin());
    >
    > /* Prints nothing, why? */
    > std::cout << std::endl << s << std::endl;
    > }
    >
    > The second call to reverse_copy doesn't work as expected because when I
    > print s I get nothing. Using a debugger, I inspect s just before and
    > right after the call to reverse_copy:
    > (gdb) p s
    > $1 = {static npos = 4294967295, _M_dataplus = {<allocator<char>> =
    > {<new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p =
    > 0x44212c ""}}
    > (gdb) n
    > (gdb) p s
    > $2 = {static npos = 4294967295, _M_dataplus = {<allocator<char>> =
    > {<new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p =
    > 0x71254c "olleh"}}
    > (gdb)
    >
    > So it seems to contain the reversed string, but it won't output?!
    > What's going on?
    >
    > - Eric


    Writing to s.begin() is wrong - it overwrites items in s starting with
    first item. But s is currently empty, so it may write boyond the
    allocated area.
    Ondra Holub, Jan 17, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page