Connecting Tech Pros Worldwide Forums | Help | Site Map

Avoiding copy: char* -> istringstream

mathieu
Guest
 
Posts: n/a
#1: Jul 22 '06
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


mlimber
Guest
 
Posts: n/a
#2: Jul 22 '06

re: Avoiding copy: char* -> istringstream


mathieu wrote:
Quote:
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

Howard Gardner
Guest
 
Posts: n/a
#3: Jul 22 '06

re: Avoiding copy: char* -> istringstream


mathieu wrote:
Quote:
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."
mathieu
Guest
 
Posts: n/a
#4: Jul 22 '06

re: Avoiding copy: char* -> istringstream


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;
}

mlimber wrote:
Quote:
mathieu wrote:
Quote:
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
mathieu
Guest
 
Posts: n/a
#5: Jul 22 '06

re: Avoiding copy: char* -> istringstream



Howard Gardner wrote:
Quote:
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

Jacek Dziedzic
Guest
 
Posts: n/a
#6: Jul 22 '06

re: Avoiding copy: char* -> istringstream


mathieu wrote:
Quote:
Howard Gardner wrote:
>
>
Quote:
>>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.
That's premature optimization, which is evil, unless
you've proven that this part is a bottleneck in your program.

HTH,
- J.
davidrubin@warpmail.net
Guest
 
Posts: n/a
#7: Jul 23 '06

re: Avoiding copy: char* -> istringstream



Jacek Dziedzic wrote:
Quote:
mathieu wrote:
Quote:
Howard Gardner wrote:

Quote:
>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.
>
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.

mathieu
Guest
 
Posts: n/a
#8: Jul 24 '06

re: Avoiding copy: char* -> istringstream



Jacek Dziedzic wrote:
Quote:
mathieu wrote:
Quote:
Howard Gardner wrote:

Quote:
>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.
>
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

Closed Thread