Redirect both cout and stdout to a string in C++ for Unit Testing

std::stringstream may be what you’re looking for. UPDATE Alright, this is a bit of hack, but maybe you could do this to grab the printf output: char huge_string_buf[MASSIVE_SIZE]; freopen(“NUL”, “a”, stdout); setbuf(stdout, huge_string_buffer); Note you should use “/dev/null” for linux instead of “NUL”. That will rapidly start to fill up huge_string_buffer. If you want to … Read more

Dead code detection in legacy C/C++ project [closed]

You could use a code coverage analysis tool for this and look for unused spots in your code. A popular tool for the gcc toolchain is gcov, together with the graphical frontend lcov (http://ltp.sourceforge.net/coverage/lcov.php). If you use gcc, you can compile with gcov support, which is enabled by the ‘–coverage’ flag. Next, run your application … Read more

Implicit int return value of C function

From the ’89 standard as quoted in the new testament: Flowing off the end of a function is equivalent to a return with no expression. In either case, the return value is undefined. That standard usually expresses the on-the-ground behavior of pre-existing implementations.