I am wondering if it is possible to convert vector of pairs into a byte array.
Here's a small example of creating the vector of pairs:
int main(int argc, char *argv[])
{
PBYTE FileData, FileData2, FileData3;
DWORD FileSize, FileSize2, FileSize3;
/* Here I read 3 files + their sizes and fill the above variables. */
//Here I create the vector of std::pairs.
std::vector<std::pair<PBYTE, DWORD>> DataVector
{
{ FileData, FileSize }, //Pair contains always file data + file size.
{ FileData2, FileSize2 },
{ FileData3, FileSize3 }
};
std::cin.ignore(2);
return 0;
}
Is it possible to convert this vector into a byte array (for compressing, and writing to disk, etc)?
Here is what I tried, but I didn't get even the size correctly:
PVOID DataVectorArr = NULL;
DWORD DataVectorArrSize = DataVector.size() * sizeof DataVector[0];
if ((DataVectorArr = malloc(DataVectorArrSize)) != NULL)
{
memcpy(DataVectorArr, &DataVector[0], DataVectorArrSize);
}
std::cout << DataVectorArrSize;
//... Here I tried to write the DataVectorArr to disk, which obviously fails because the size isn't correct. I am not also sure if the DataVectorArr contains the DataVector now.
if (DataVectorArr != NULL) delete DataVectorArr;
Enough code. Is is it even possible, or am I doing it wrong? If I am doing it wrong, what would be the solution?
Regards, Okkaaj
Edit: If it's unclear what I am trying to do, read the following (which I commented earlier):
Yes, I am trying to cast the vector of pairs to a PCHAR
or PBYTE
- so I can store it to disk using WriteFile. After it is stored, I can read it from disk as byte array, and parse back to vector of pairs. Is this possible? I got the idea from converting / casting struct
to a byte array and back(read more from here: Converting struct to byte and back to struct) but I am not sure if this is possible with std::vector instead of structures.