Note that there are some explanatory texts on larger screens.

plurals
  1. PORandom Bytes added to an end of a buffer
    text
    copied!<p>I was making a re-creation of some System.IO functions from that class. When I setup a buffer and allocate n number of bytes it'll read bytes to that and then add random bytes to the end of that buffer.</p> <p>For example:</p> <p>My Main:</p> <pre><code>int main(int argc, char *args[]) { SetConsoleTitle(TEXT("Stream Test.")); cout&lt;&lt;"Press any Key to begin reading."; cin.get(); const char* data = File::ReadAllBytes(args[1]); Stream* stream = new Stream(data); char* magic = new char[8]; stream-&gt;Read(magic, 0, 8); magic[8] = '\0'; cout&lt;&lt;magic&lt;&lt;endl&lt;&lt;endl; delete[]data; cout&lt;&lt;"Press any key to quit."; cin.get(); return 0; } </code></pre> <p>and here is my System::IO namespace + stream class:</p> <pre><code>namespace System { namespace IO { class File { public: static char* ReadAllBytes(const char *name) { ifstream fl(name, ifstream::in|ifstream::binary); fl.seekg( 0, ifstream::end ); size_t len = fl.tellg(); char* ret = new char[len+1]; ret[len] = '\0'; fl.seekg(0); fl.read(ret, len); fl.close(); return ret; } //not sure of this use yet. static size_t fileSize(const char* filename) { ifstream in(filename, ifstream::in | ifstream::binary); in.seekg(0, ifstream::end); return in.tellg(); } }; class Stream { public: const char *_buffer; __int64 _origin; __int64 _position; __int64 _length; __int64 _capacity; bool _expandable; bool _writable; bool _exposable; bool _isOpen; static const int MemStreamMaxLength = 2147483647; Stream() { InitializeInstanceFields(); } Stream(const char *buffer) { _buffer = buffer; _length = strlen(_buffer); _capacity = _length; _position = 0; _origin = 0; _expandable = false; _writable = true; _exposable = true; _isOpen = true; } int ReadByte() { if (_position &gt;= _length) return -1; return _buffer[_position++]; } void Read(char* &amp;buffer, int offset, int length) { if((_position + offset + length) &lt;= _length) { memcpy( buffer, _buffer + (_position + offset), length ); _position += length; } } private: void InitializeInstanceFields() { _origin = 0; _position = 0; _length = 0; _capacity = 0; _expandable = false; _writable = false; _exposable = false; _isOpen = false; } }; } } </code></pre> <p>This is what ends up happening: <img src="https://i.stack.imgur.com/pGoT9.png" alt="What happens"></p> <p>Can anyone explain why this happens, how I can fix, or anything else? I'm new to C++ so any explanations would help. Also please don't criticize my scripting, I know it may be bad, outdated, deprecated, etc. but I'm open to learning and any helping advice goes for the better. :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload