extracting rest of an istream (istrstream)

J

Johannes Zellner

Hi,

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

Gavin Deane


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
 
K

Kai-Uwe Bux

Gavin said:

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
 
M

Mike Wahler

Johannes Zellner said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top