user3480782
user3480782

Reputation: 3

Split array into chunks in C/C++

I am looking for a way to split an array into multiple arrays in C/C++, with standard Win32 and C/C++ functions.

Here's an example, how I read a file into the array.

using namespace std;

LPSTR File_To_Read = "FILE.exe"; 
DWORD File_To_Read_Size = NULL;
DWORD inputSize = NULL;
PBYTE inputData = NULL;

ifstream input(File_To_Read, ifstream::binary | ifstream::ate);
File_To_Read_Size = static_cast<DWORD>(input.tellg());
input.seekg(input.beg);

inputData = new BYTE[File_To_Read_Size];
input.read(reinterpret_cast<char*>(inputData), File_To_Read_Size);
input.close();

Now I want to split the inputData like this.

DWORD inputSize_part1;
DWORD inputSize_part2;
DWORD inputSize_part3;

PBYTE inputData_part1;
PBYTE inputData_part2;
PBYTE inputData_part3;

So in the later I can also put them back together.

How should I proceed? I would show an example code what I have tried, but my code would not make not much sense for you experts.

Edit: @IKH Chunk sizes should be around the same size. So if inputData is 33kb, then inputData_part1(and inputSize_part1) should be 11kb, inputData_part2(and inputSize_part2) should be 11kb, and so on. So in the end there would be 3x 11kb arrays and DWORDS for their sizes.

Upvotes: 0

Views: 3009

Answers (2)

Kenneth McKanders
Kenneth McKanders

Reputation: 122

The actual chunk size shouldn't matter. Following up on @pmg's comment, after the line input.read(reinterpret_cast<char*>(inputData), File_To_Read_Size);, you have a single array to store the data. The size of the shortest array will always be File_To_Read_Size / 3 because that is the number of arrays you are splitting it into. For access, the index used would be slightly augmented by the index of the chosen array, like so:

//assume array_index is the number of the array you are accessing (1,2, or 3)
//and position is the location you are trying to access in that array
BYTE result = inputData[position + (array_index - 1)];

Using this, all you need to know is what File_To_Read_Size % 3 is. If File_To_Read_Size % 3 == 1, then the first array contains the last BYTE. Else if File_To_Read_Size % 3 == 2, then the second array contains the last BYTE. Otherwise, all the arrays are the same size. Each of the first two ifs would thus be a check for whether the index is valid.

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249592

DWORD inputSize_part1 = inputSize / 3;
DWORD inputSize_part2 = inputSize / 3;
DWORD inputSize_part3 = inputSize - inputSize_part1 - inputSize_part2;

PBYTE inputData_part1 = inputData;
PBYTE inputData_part2 = inputData + inputSize_part1;
PBYTE inputData_part3 = inputData + inputSize_part1 + inputSize_part2;

Now you have three pointers and three sizes for the three chunks: the first two are a third or slightly less, and the third chunk may be slightly larger if the original was not evenly divisible. You'll need to delete[] inputData when you're doing using all of the chunks.

Upvotes: 1

Related Questions