std::reverse_copy() woes

E

Eric Lilja

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
 
O

Ondra Holub

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));
 
O

Ondra Holub

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top