473,386 Members | 1,699 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,386 software developers and data experts.

extracting rest of an istream (istrstream)

Hi,

how can I extract the whole rest of an istream? (All characters
which have not been consumed yet?)

--
Johannes
Jan 20 '06 #1
4 3523

wi******@hotmail.com wrote:
See this example:
http://www.cplusplus.com/ref/iostream/istream/read.html


int main () {
<snip>
char * buffer;
<snip>
buffer = new char [length];
<snip>
}

No, I didn't snip the delete []. They just left it out.

Some might say that it doesn't matter because the example isn't about
memory management. Or that it's a simple main function that ends
straight away and any reasonable OS will reclaim the memory.

But that assumes that a novice reading the code already knows about
memory management, or will never cut and paste that code into their own
function and call it 10000 times.

The mistake in the example can only be caused by laziness or
incompetence. I wouldn't expect my employer to accept that sort of
laziness and I don't think it should be accepted in code examples here
either.

Not the first time I've seen rubbish on cplusplus.com.

std::vector<char> renders the whole question moot anyway.

Gavin Deane

Jan 20 '06 #3
Gavin Deane wrote:

wi******@hotmail.com wrote:
See this example:
http://www.cplusplus.com/ref/iostream/istream/read.html


int main () {
<snip>
char * buffer;
<snip>
buffer = new char [length];
<snip>
}

No, I didn't snip the delete []. They just left it out.

Some might say that it doesn't matter because the example isn't about
memory management. Or that it's a simple main function that ends
straight away and any reasonable OS will reclaim the memory.

But that assumes that a novice reading the code already knows about
memory management, or will never cut and paste that code into their own
function and call it 10000 times.

The mistake in the example can only be caused by laziness or
incompetence. I wouldn't expect my employer to accept that sort of
laziness and I don't think it should be accepted in code examples here
either.


Now, would that not make for an excellent interview question:

Question
--------
Consider the following two programs:

(a)

#include <iostream>

int main ( void ) {

char* msg = new char;
*msg = '\n';
std::cout << *msg;

}
(b)

#include <iostream>

int main ( void ) {

char* msg = new char;
*msg = '\n';
std::cout << *msg;
delete msg;

}

According to the C++ standard, describe the differences in semantics (not
style!) between the programs (a) and (b).

My shot at the answer:
----------------------

There are none. According to the standard, evaluating the expression

delete msg;

involves two things: (1) the destructor of *msg is called and (2) the memory
for the now destroyed object is "deallocated" by calling a deallocation
function (in our example the global default deallocation function). Since
the destructor of a char object is trivial, we are left to discuss the
possible effects of the memory management.

The only guarantee that is made for the side effects of the deallocation
function made by the standard is that all pointer refering to any part of
the deallocated storage are rendered invalid. No promise (in the form of,
say, a post-condition) is made that deallocated storage be reused in any
form. However, the standard states that the purpose of the deallocation
function is to "reclaim" the memory. However, that cannot be construed as
returning memory to the OS: the standard makes no assumptions that the OS
has facilities for memory management (in fact, the standard barely makes
any assumptions about the execution environment at all). Thus, in the
context of the standard, the phrase "to reclaim memory" suggests, at best,
that deallocated memory may be reused by the same program.

Since there are no further allocations in program (b) following the delete
statement, all memory management related side effects of the delete
statement are unobservable. According to the as-if rule, the compiler is
entitled to generating identical code for both programs.
Best

Kai-Uwe Bux
Jan 20 '06 #4
"Johannes Zellner" <jo******@zellner.org> wrote in message
news:sl*********************@orbital.zellner.org.. .
Hi,

how can I extract the whole rest of an istream? (All characters
which have not been consumed yet?)


#include <istream>
#include <iostream>

void f(std::istream& is)
{
char c(0);
while(is.get(c))
;

if(!(is.eof())
std::cerr << "Error occurred during reading\n";
else
std::cout << "End of stream reached\n";
}
-Mike
Jan 20 '06 #5

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

Similar topics

6
by: Ahmed Ossman | last post by:
Hi All, I have this piece of code: char str; ...... double x; istrstream in(str); in >> x;
8
by: Frank Nielsen | last post by:
Can istrstream contain binary data like '\0' and other non ascii characters???? My program received a byte array through jni (java) and i must put it in a stream to continue the flow in the...
2
by: Ney André de Mello Zunino | last post by:
Hello. Is g++ 3.3.3 (Cygwin) correct in rejecting the following test program? #include <istream> #include <sstream> #include <fstream> void test(std::istream& source) {
2
by: Aleander | last post by:
Hi! I have to write the records of a vector in a file, e and then open this file to extract the record to refill the vector. My program has two class: Visita(Appointment) and Data(date). The...
6
by: Kifah Abbad | last post by:
Ok guys, here is the deal, i hope someone can help me out with this. I receive a string through a socket...i dunno how long this string is, but i save it all into a buf. .. .. ..
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
4
by: dor | last post by:
i have an input file named input.txt where all the data looks like this: (4,10) 20 (5,3) 13 (7,19) 6 .. .. .. the numbers are random. i need to use every number in each line
3
by: mos | last post by:
Hi! the question can describle as code: //os like "mike head hello world!" string GetRest(istringstream& os) { string name,part; //and maybe some other element os >> name >> part;...
7
by: John Salmon | last post by:
I'm working with two libraries, one written in old school C, that returns a very large chunk of data in the form of a C-style, NUL-terminated string. The other written in a more modern C++ is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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,...

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.