I would like to create a sub array which is offset from the original array without copying the contents.
i.e
byte* arr = new byte[4];
//0...3 meaning 0 through 3
arr[0...3] = 1...4
byte* subarr = arr+2;
So that subarr[0] is equal to 3. This actually does work; if I print subarr[0] I get 3. However if I want to use it in a function such as memcpy(), I get an error:
There isn't actually an error message, but the breakpoint is in free_base.cpp. The breakpoint in my code is at memcpy. The actual use case is:
byte* tempbuffer = new byte[size+2];
ReadProcessMemory(processHandle, (PBYTE*)(moduleStart + offset - 1), tempbuffer, size+2, 0);
byte* subbuffer = tempbuffer + 1;
assert(subbuffer[0] == tempbuffer[1]);
memcpy(out_buffer, subbuffer, size);
delete[] subbuffer;
delete[] tempbuffer;
I know the sub array in this example is redundant because I can literally just pass the buffer into ReadProcessMemory however this was just to find the bug. If I pass out_buffer (which is an LPVOID) directly to ReadProcessMemory I get valid results and no errors. Also if I use memcpy on tempbuffer (the original array), it also gives me what I want with no errors. So the error must be caused by the sub array and using memcpy on it.
Why is this happening and what can I do as an alternative?
Thanks.
delete[] subbuffer;You shouldn't destroy anything not comming directly fromnew.