tags:

views:

54

answers:

4

When i use << operator to put values in buffer sometimes the address of buffer changes. does it copy the values to new address or move them to a new address in memory and how can i know the address where data is stored in?

Also, I'd be interested in a book that covers about streams in c++.

+2  A: 

If you put more data into a buffer then it can hold, the stream class will automatically reallocate a bigger buffer and move all the current data into the new one.

The entire point of the iostreams class is that you are not supposed to worry about those details.

For a book on just IOStream: Try Standard C++ IOStreams and Locales: Advanced Programmer's Guide and Reference by Angelika Langer (who I've met and trust) (The forward is by Jerry Schwartz, who wrote the first implementation of iostreams. He wrote a book on the topic as well, but it seems to be out of print)

James Curran
Well you do if you saved the buffer address for something else _before_ adding some text in it.EG: `ptr = s; s << "additionnaltext"; // ptr might be wrong now`
Gui13
A: 

About the book, just to mention one, Thinking in C++, volume 2 (chapter 4. Iostreams), by Bruce Eckel.

rturrado
+1  A: 

As far as books go, it all depends on the level of detail you want. The C++ Standard Library (Nicolai Josuttis) does a good job as far as using iostreams goes.

If you want to know about implementation details (e.g., you're out to implement your own iostreams), Standard C++ Iostreams and locales (Langer and Kreft) is the seminal work on the subject.

Jerry Coffin
+1  A: 

This is in the class of gotchas such as

std::vector<int> v;
v.push_back(1);
v.push_back(v.back());

or

std::string s("x");
s.append(s.c_str());

mutating and referring to an address within an STL container can cause memory errors as the container treats the memory location as a private resource but unfortunately in the name of performance the abstraction is not water-tight. I got bitten by the vector push back problem and it took me ages to figure out.

bradgonesurfing