I am reading through a buffer (char *) and i have a cursor, where i am tracking my starting position of the buffer, is there a way to copy characters 7-64 out of the buffer, or is my best bet to just loop the buffer from poistion x to position y?
The size of the destination buffer is the result of another function dynamically computed
Initializing this returns
variable-sized object 'version' may not be initialized
Relevant code parts:
int32_t size = this->getObjectSizeForMarker(cursor, length, buffer);
cursor = cursor + 8; //advance cursor past marker and size
char version[size] = this->getObjectForSizeAndCursor(size, cursor, buffer);
–
char* FileReader::getObjectForSizeAndCursor(int32_t size, int cursor, char *buffer) {
char destination[size];
memcpy(destination, buffer + cursor, size);
}
–
int32_t FileReader::getObjectSizeForMarker(int cursor, int eof, char * buffer) {
//skip the marker and read next 4 byes
cursor = cursor + 4; //skip marker and read 4
unsigned char *ptr = (unsigned char *)buffer + cursor;
int32_t objSize = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
return objSize;
}
Move the pointer to
buffersix units ahead (to get to the seventh index), and thenmemcpy64-7 (57) bytes, e.g.:You may want to terminate the
destinationarray so that you can work with it using standard C string functions. Note that we’re adding the null character at the 58th index, after the 57 bytes that were copied over:If you need to work with a dynamically sized
destination, use a pointer instead of an array:Honestly, if you’re in C++, you should really probably be using the C++ strings library and
std::stringclass. Then you can call thesubstrsubstring method on yourstringinstance to get the 57-character substring of interest. It would involve fewer headaches and less re-inventing the wheel.But the above code should be useful for both C and C++ applications.