473,395 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Avoiding copy: char* -> istringstream

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

Jul 22 '06 #1
7 3148
mathieu wrote:
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

Jul 22 '06 #2
mathieu wrote:
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."
Jul 22 '06 #3
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:
mathieu wrote:
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
Jul 22 '06 #4

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

Jul 22 '06 #5
mathieu wrote:
Howard Gardner wrote:

>>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.
Jul 22 '06 #6

Jacek Dziedzic wrote:
mathieu wrote:
Howard Gardner wrote:

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

Jul 23 '06 #7

Jacek Dziedzic wrote:
mathieu wrote:
Howard Gardner wrote:

>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

Jul 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christian Seberino | last post by:
I wrote some C functions and successfully imported them into Python. All is apparently well but there is one question about the object returned by the C function that is bugging me... Take for...
4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
22
by: Shea Martin | last post by:
I have a String class (I know I am re-inventing the wheel, yes I have heard of boost, and of QString). My copy constructor does a deep (strcpy) of the char *_buffer member. I have a member...
2
by: Craig Nicol | last post by:
Hi everyone, I'm looking for some help migrating my code from Microsoft VC++ 6.0, which was not the most conformant compiler I have now discovered. I'm trying to move to .NET and I've made the...
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
1
by: Pedro Graca | last post by:
Well ... I've been using Windows for a long time before I switched to Linux. I'm very used to type CTRL-ALT-DEL everytime I'm leaving my computer or arriving to it. It's hapenned _twice_ (in the...
11
by: csudha | last post by:
Hi all, Can you please explain me the usage of copy constructor and how do we use it. Give me some example. Regards, Sudha.
4
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple...
6
by: Gary | last post by:
I'm confused about the two ways of string copy below. The first one (two functions) works; the second doesn't. Can anybody explain the reason to me? Thanks a lot. #include <stdio.h> /*this one...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.