I am using an ostringstream object in an application that runs for a long time to print debug information to standard out. I toggle whether the application actually prints and clears the ostringstream object based on a command line arg (e.g., verbose flag). When the verbose switch is not asserted I still write to the ostringstream object but I never clear it.
I am trying to figure out how bad this is and whether I should take more care on clearing the object? Are there any negative repercussions such as using too much memory?
// example code
ostringstream oss;
while (1){
oss << " still alive " << endl;
if (verbose) { cout << oss.str(); oss.str("") }
}
Obviously when you keep inserting data in the stream it'll consume more memory, which at some point can be a lot.
Clearing it will prevent that.
Related
Programm_A
int main()
{
std::cout << "Process ID = " << GetCurrentProcessId() << "\n"; // Id my process (i get something like '37567')
std::string My_String = "JoJo"; // My string
std::cout << &My_String << std::endl; //here i get something like '0x0037ab7'
system("pause");
}
This program just outputs reference of string "JoJo" to console.
Programm_B
int main()
{
int id;
std::cin >> id;
DWORD ProcessId = id;
HANDLE ProcessHandle = OpenProcess(PROCESS_VM_READ, FALSE, ProcessId);
if (!ProcessHandle) {
std::cout << "Process is not found...\n";
return 0;
}
std::string r;
std::cin >> r; // this is me writing the link that I get in programs_A
DWORD address = std::strtoul(r.c_str(), NULL, 16);
std::string JoJo_string = " ";
ReadProcessMemory(ProcessHandle, (LPVOID)(address), &JoJo_string, sizeof(JoJo_string), 0); //here I want to get the JoJo_string value by reference from programm_A
std::cout << JoJo_string << std::endl;
}
The funny thing is that everything works fine with the "int" variable type. But std::string is not working. The exact value reads, but the program immediately gives an error:
[-- programm_B --]
[-- error --]
You can't easily read a std::string across process boundaries.
Different standard library implementations of std::string use different memory layouts for its data members. The std::string in your program may be using a different implementation than the program you are trying to read from.
But even if the two programs used the exact same implementation, it still wouldn't matter, because std::string is simply too complex to read with a single ReadProcessMemory() call. std::string uses dynamic memory for its character data, so one of its data members is a char* pointer to the data stored elsewhere in memory. Which is complicated by the fact that std::string might also implement a local Short-String Optimization buffer so short string values are stored directly in the std::string object itself to avoid dynamic memory allocations.
So, you would have to know the exact implementation of std::string being used by the target program in order to decipher its data members to discover where the character data is actually being stored, and in the case where SSO is not active then read the char* pointer, as well as the number of characters being pointed at (which itself is also determinate in an implementation-specific way), so you could then read the character data with another ReadProcessMemory() call.
In short, what you are attempting to do is a futile effort.
You most certainly CAN get a content of the std::string from another process. After all, the debuggers do that!
You need to execute data() and length() functions of that string and then read content of that memory. The important point is to halt execution of the second process while you are doing that, or that memory location can become invalid between your calls.
I was surprised to see my program suddenly go quiet when I added a cout at some point, so I isolated the responsible code:
std::stringstream data;
data<<"Hello World\n";
std:std::fstream file{"hello.txt", std::fstream::out};
file<<data.rdbuf();
std::cout<<"now rdbuf..."<<std::endl;
std::cout<<data.rdbuf()<<std::endl;
std::cout<<"rdbuf done."<< std::endl;
The program quietly exits without the final cout. What is going on? If I change the last .rdbuf() to .str() instead then it completes.
During the call to std::cout<<data.rdbuf(), std::cout is unable to read any characters from data's filebuf because the read position is already at the end of the file after the previous output; accordingly, this sets failbit on std::cout, and until this state is cleared any further output will fail too (i.e. your final line is essentially ignored).
std::cout<<data.str()<<std::endl; will not cause cout to enter a failed state because data.str() returns a copy of the underlying string regardless of where the read position is (for mixed-mode stringstreams anyway).
I am creating a simulated mobile network using named pipes for information transportation between processes. I had the idea of creating a frame class to hold the data I needed and to pass the address of that frame into the pipes. The only thing I cant figure out to save my life is how to convert the address of my frame into a character pointer in order to pass it into the write() function. This is an outline of what I have so far:
NFrame *frame = new NFrame;
// set frame information
char *f_address = (char*)(frame);
std::cout << f_address << std::cout;
whenever I call cout to verify the address all I get are unreadable characters. I've messed with reinterpret_cast and I run into the same situation.
What am I missing or am I even doing this right.
What you probably want is serialization. It is often preferable (for ease of debugging) to serialize in some textual format like JSON. You could use libraries like JsonCPP for that. See also s11n which is a binary serialization framework (but I recommend JSON).
You usually should not write the raw memory bytes of some complex C++ object, because they contain data which would be unreadable by most other programs and processes (e.g. internal pointers to your process; in another process running even the same program, they are likely to be different because of ASLR).
(You might write raw memory bytes if you are sure they are read by the same process; but even that could be erroneous for C++ programs)
If (e.g. for debugging purposes) you want to simply show the address of some heap allocated object, you might convert it to (void*) like:
std::cout << "frame#" << (void*)frame << std::endl;
but such addresses (often in hexa) are not very readable and of course don't tell aything about the internal state of your frame.
You could define (by overloading the output << operator) your own output << operator like
std::ostream& operator <<(std::ostream&, const NFrame&);
then code std::cout << (*frame) << std::endl
Of course, you can replace std::cout by some instance of std::ofstream to output into some file, or some instance of std::ostringstream to output into a string.
An address will have a lot of unprintable characters in it. What you probably want is to convert the address into raw bytes and then convert that into a hexadecimal string.
That's because cout 'thinks' you are passing it a string which is presented as char *.
Try this:
std::cout << reinterpret_cast<void*>(f_address) << std::endl;
I am new to C++ and currently using visual studio.
I see on many tutorials online that when reading from a file with ifstream, before we cout the contents when looping through each line, we write the contents of the current line to a char data[] var then cout the data.
Something like the following
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream f("D:\\file1.txt");
char a[80];
while(!f.eof())
{
f >> a;
cout << a << endl;
}
return(0);
}
What is the point of
char a[80];
...
f >> a;
cout << a << endl;
When we could just do
cout << f << endl;
And save declaring a char and wasting more lines of code?
The preferred method in C++ is:
#include<iostream>
#include<fstream>
int main()
{
std::ifstream f("D:\\file1.txt");
if( f )
{
std::string line;
while(getline(f, line) )
{
std::cout << line << endl;
}
}
return 0 ;
}
If you want to copy, or list files, use operating system commands. The operating system commands are more optimized for handling files. Also, they already exist and have been tested so you don't waste your time.
What is the purpose of storing the content in memory before printing it?
In your example, there isn't much of a point. The data being read from std::cin is being sent directly to std::cout to be displayed on the console. Generally, the only reasons you'd want to store the data in the program memory before printing it is if you want to modify the data or check properties of the data and take certain actions based on those properties.
It should be noted that while this is a common example, the use of while (!eof()) is the incorrect way to check the validity of the stream before reading data. This method checks the stream before the input is read, which can lead to undefined behavior if invalid data is read and subsequently used. The normal way to read data is to check the validity of the stream after performing the read. For example, in your program this would be changed to:
while (f >> a)
{
std::cout << a << std::endl;
}
After the read is performed, the stream will be converted to a boolean. It will return true or false depending on the validity of the stream. If the stream read the end-of-file (EOF) character then that would be a failed read and the stream will return false.
What is the point of
char a[80];
...
f >> a;
cout << a << endl;
when we could just do
cout << f << endl;
First, cout << f will not do what you expect. The stream insertion operator (operator<<()) is overloaded for certain types. f is of type std::ifstream - a type for which this operator is not overloaded. Pre-C++11 C++ IOStreams contained a conversion to void* so that they could be used in boolean contexts. The stream insertion operator is overloaded for pointers to void, so the output you would get is not something you'd expect. As of C++11 you'd get a compiler error that no operator overload could be found for that type.
There is, however, an overload for std::streambuf*, a pointer an IOStreams buffer. Each stream has a buffer that stores and maintains characters from the source or sink. The overload for this operator reads data from the buffer and sends it to its own buffer, so you can do something like this:
std::cout << f.rdbuf();
rdbuf() returns a pointer to the stream's buffer.
While this is an effective use of the stream's capabilities, the data is still being stored in the buffer of std::cout. Streams are buffered and data sent into the source or sink are consigned to a buffer where it waits until the buffer is flushed. You can use std::nounitbuf to unbuffer std::cout in order to write directly to the external device:
std::cout << std::nounitbuf
<< f.rdbuf();
For a simple example with a small file, buffering really isn't needed. If you have a very large file then buffering is very useful as the program doesn't have to make a system call for each character being inserted.
I'd like to provide an std::ostream that may or may not, from the user's point of view, encrypt its contents.
Imagine some random function that uses an std::ostream&:
void write_stuff( std::ostream& stream ) {
os << "stuff";
}
Whether stuff is output in cleartext or is encrypted is dependent on how the stream argument was initialized.
I'm thinking about inheriting from std::basic_streambuf, but I'm not sure it's the best way to go. I think the ideal scenario is some sort of filter object which is run before the contents are actually output. That way it'd be easy to chain operations, e.g. encrypting and then hex-encoding, which is a benefit that making my own basic_streambuf does not seem to give (not easily at least).
Any advices?
Thanks.
UPDATE: I followed the strategy laid out by the accepted answer. I found this article on writing a custom streambuf which performs XOR obfuscation to be extremely helpful as a starting point.
A streambuf can be implemented in terms of another arbitrary streambuf that's either passed in as an argument to the constructor or set with a special member function. And that allows you to stack streambuf implementations like you were thinking of.
You could even make a manipulator that uses the ostream's rdbuf function to get the current streambuf, call a function on your streambuf that sets the 'stacked' streambuf, and then calls rdbuf again to replace the ostream's streambuf with your own.
aes_ctr_streambuf encrypter(key);
zlib_streambuf compressor;
::std::cout << stack_streambuf(encrypter) << stack_streambuf(compressor);
It is hard to describe in short what you have to do in order to create an I/O stream for the new source or sink. Luckily, Jonathan Turkanis and CodeRage, LLC have created very nice building blocks with exceptional documentation that can help you save hundreds of hours of research and development of new streams. The library is called Boost.Iostreams.
The second example in documentation exactly shows how to solve your problem.
Also, note that they already have an output stream with zip compression. So maybe you don't even need to write a single line of code.
Good luck!
I'm not sure that what I'm about to suggest is exactly what you need (and its too complicated to be a comment), but have you ever heard of the stream manipulators ?
#include <iostream>
#include <iomanip>
int main(int, char**)
{
int a = 10;
std::cout << "a: " << a << std::endl; //outputs: "a: 10"
std::cout << "a: " << std::hex << a << std::endl; //outputs: "a: a"
std::cout << "a: " << std::dec << a << std::endl; //outputs: "a: 10"
return EXIT_SUCCESS;
}
You may indeed use a similar pattern (this matches what you actually call a "filter" object) to somehow change the state of your custom stream object.