Avoiding copy: char* -> istringstream

M

mathieu

Hello,
Is there a way to construct a std::istringstream from a buffer of
char and avoid copying the array of bytes ?

const char s[] = "Hello, World";
std::string str(s);
std::istringstream is;
is.str( str );


Thanks
Mathieu
 
M

mlimber

mathieu said:
Is there a way to construct a std::istringstream from a buffer of
char and avoid copying the array of bytes ?

const char s[] = "Hello, World";
std::string str(s);
std::istringstream is;
is.str( str );

How about using the constructor:

istringstream is( "Hello, world" );

Cheers! --M
 
H

Howard Gardner

mathieu said:
Hello,
Is there a way to construct a std::istringstream from a buffer of
char and avoid copying the array of bytes ?

const char s[] = "Hello, World";
std::string str(s);
std::istringstream is;
is.str( str );


Thanks
Mathieu

There are implementations of std::string that will not make the copy
until you change the value of the string. This is an implementation
choice, though, not a standard mandated behavior. The choice is called
"copy on write."
 
M

mathieu

Does not work, at least not with my version of gcc...

Thanks anyway
Mathieu

#include <sstream>
#include <iostream>

int main()
{
const char d[] = "Hello, World";
char *s = new char[strlen(d)+1];
strcpy(s, d);
std::istringstream is( s );
std::cout << is.str() << std::endl;
std::cout << s << std::endl;
s[7] = 'B';
std::cout << is.str() << std::endl;
std::cout << s << std::endl;

delete[] s;

return 0;
}
mathieu said:
Is there a way to construct a std::istringstream from a buffer of
char and avoid copying the array of bytes ?

const char s[] = "Hello, World";
std::string str(s);
std::istringstream is;
is.str( str );

How about using the constructor:

istringstream is( "Hello, world" );

Cheers! --M
 
M

mathieu

Howard said:
There are implementations of std::string that will not make the copy
until you change the value of the string. This is an implementation
choice, though, not a standard mandated behavior. The choice is called
"copy on write."

Good to know ! Thanks

I guess I simply derive from std::istream and provide my implementation
of a 'istringstream' like interface.

Mathieu
 
J

Jacek Dziedzic

mathieu said:
Howard Gardner wrote:




Good to know ! Thanks

I guess I simply derive from std::istream and provide my implementation
of a 'istringstream' like interface.

That's premature optimization, which is evil, unless
you've proven that this part is a bottleneck in your program.

HTH,
- J.
 
D

davidrubin

Jacek said:
That's premature optimization, which is evil, unless
you've proven that this part is a bottleneck in your program.

In any case, it's straightforward to implement a streambuf that does
not make a copy. Then, you can use it with any stream.
 
M

mathieu

Jacek said:
That's premature optimization, which is evil, unless
you've proven that this part is a bottleneck in your program.

I could be manipulating up to 4Gb of data in this buffer...

M
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top