I'm writing a debugging tool in C . The tool is not allowed to use the malloc heap, because doing so might alter the behavior of the program being debugged. Instead, the debugging tool has its own heap, separate from the malloc heap (let's call it the "debugger's heap").
My debugging tool is making heavy use of the C STL 'Allocator' parameter, to ensure that all my data structures go into the debugger's heap. So far, this is working fine.
Now I need to write an output file to disk. I'm pretty sure that opening a std::ofstream would allocate memory on the heap. I mean, the file buffers have to go somewhere. But the std::ofstream doesn't accept an Allocator parameter. Is there any way to open an output file, and put the file buffers into the "debugger's heap"?
CodePudding user response:
Yes, you can't use streams in your scenario.
On most platforms, you can use the POSIX open
function and then call read
, write
and close
as appropriate. On Windows they renamed it _open
, but it's basically the same.
These functions are unbuffered, so incur no heap allocations. On the other hand, if you perform a lot of small reads or writes your program will be slower because of the lack of buffering, so you need to take this into account when designing your code.