#include <iostream>
#include <string.h>
using namespace std;
class myClass
{
public :
myClass();
~myClass();
struct DynamicArray
{
uint8_t * data;
size_t size;
};
void addToArray( DynamicArray & array, uint8_t * data, size_t len );
DynamicArray myDynamicArray1;
DynamicArray myDynamicArray2;
};
myClass::myClass() :
myDynamicArray1( { nullptr, 0 } ),
myDynamicArray2( { nullptr, 0 } )
{
}
myClass::~myClass()
{
if ( myDynamicArray1.data != nullptr )
{
delete[] myDynamicArray1.data;
}
if ( myDynamicArray2.data != nullptr )
{
delete[] myDynamicArray2.data;
}
}
void myClass::addToArray( DynamicArray & array, uint8_t * data, size_t len )
{
DynamicArray tmp;
tmp.size = array.size + len;
tmp.data = new (nothrow) uint8_t[ tmp.size ];
if ( tmp.data != nullptr )
{
if ( array.data != nullptr )
{
memcpy( tmp.data, array.data, array.size );
delete[] array.data;
}
memcpy( tmp.data + array.size, data, len );
array.data = tmp.data;
array.size = tmp.size;
}
else
{
printf("sorry, couldn't add to array, memory allocation failed!\n");
}
}
myClass test;
int main()
{
uint8_t a[] = { 10, 20, 30, 40, 50, 60, 70 };
uint8_t b[] = { 80, 90, 100 };
test.addToArray( test.myDynamicArray1, a, 7 );
test.addToArray( test.myDynamicArray2, b, 2 );
test.addToArray( test.myDynamicArray1, b, 3 );
test.addToArray( test.myDynamicArray2, a, 5 );
for ( size_t i = 0; i < test.myDynamicArray1.size; i++ )
{
printf( "test.myDynamicArray1.data[%d] = %d\n", i, test.myDynamicArray1.data[i] );
}
printf( "\n" );
for ( size_t i = 0; i < test.myDynamicArray2.size; i++ )
{
printf( "test.myDynamicArray2.data[%d] = %d\n", i, test.myDynamicArray2.data[i] );
}
printf( "\n" );
size_t totalSize = test.myDynamicArray1.size + test.myDynamicArray2.size;
uint8_t * allData = new (nothrow) uint8_t[ totalSize ];
if ( allData != nullptr )
{
memcpy( allData, test.myDynamicArray1.data, test.myDynamicArray1.size );
memcpy( allData + test.myDynamicArray1.size, test.myDynamicArray2.data, test.myDynamicArray2.size );
for ( size_t i = 0; i < totalSize; i++ )
{
printf("allData[%d] = %d\n", i, allData[i] );
}
delete[] allData;
}
delete[] test.myDynamicArray1.data;
delete[] test.myDynamicArray2.data;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBteUNsYXNzCnsKICAgIHB1YmxpYyA6CgogICAgICAgIG15Q2xhc3MoKTsKICAgICAgICB+bXlDbGFzcygpOwoKICAgICAgICBzdHJ1Y3QgRHluYW1pY0FycmF5CiAgICAgICAgewogICAgICAgICAgICB1aW50OF90ICogZGF0YTsKICAgICAgICAgICAgc2l6ZV90IHNpemU7CiAgICAgICAgfTsKCiAgICAgICAgdm9pZCBhZGRUb0FycmF5KCBEeW5hbWljQXJyYXkgJiBhcnJheSwgdWludDhfdCAqIGRhdGEsIHNpemVfdCBsZW4gKTsKCgkgICAgRHluYW1pY0FycmF5IG15RHluYW1pY0FycmF5MTsgCiAgICAgICAgRHluYW1pY0FycmF5IG15RHluYW1pY0FycmF5MjsKfTsKCm15Q2xhc3M6Om15Q2xhc3MoKSA6CiAgICBteUR5bmFtaWNBcnJheTEoIHsgbnVsbHB0ciwgMCB9ICksCiAgICBteUR5bmFtaWNBcnJheTIoIHsgbnVsbHB0ciwgMCB9ICkKewp9CgpteUNsYXNzOjp+bXlDbGFzcygpCnsKICAgIGlmICggbXlEeW5hbWljQXJyYXkxLmRhdGEgIT0gbnVsbHB0ciApCiAgICB7CiAgICAgICAgZGVsZXRlW10gbXlEeW5hbWljQXJyYXkxLmRhdGE7CiAgICB9CiAgICBpZiAoIG15RHluYW1pY0FycmF5Mi5kYXRhICE9IG51bGxwdHIgKQogICAgewogICAgICAgIGRlbGV0ZVtdIG15RHluYW1pY0FycmF5Mi5kYXRhOwogICAgfQp9CiAKdm9pZCBteUNsYXNzOjphZGRUb0FycmF5KCBEeW5hbWljQXJyYXkgJiBhcnJheSwgdWludDhf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hbWljQXJyYXkyLCBhLCA1ICk7CiAKICAgIGZvciAoIHNpemVfdCBpID0gMDsgaSA8IHRlc3QubXlEeW5hbWljQXJyYXkxLnNpemU7IGkrKyApCiAgICB7CiAgICAgICAgcHJpbnRmKCAidGVzdC5teUR5bmFtaWNBcnJheTEuZGF0YVslZF0gPSAlZFxuIiwgaSwgdGVzdC5teUR5bmFtaWNBcnJheTEuZGF0YVtpXSApOwogICAgfQogICAgcHJpbnRmKCAiXG4iICk7CiAgICBmb3IgKCBzaXplX3QgaSA9IDA7IGkgPCB0ZXN0Lm15RHluYW1pY0FycmF5Mi5zaXplOyBpKysgKQogICAgewogICAgICAgIHByaW50ZiggInRlc3QubXlEeW5hbWljQXJyYXkyLmRhdGFbJWRdID0gJWRcbiIsIGksIHRlc3QubXlEeW5hbWljQXJyYXkyLmRhdGFbaV0gKTsKICAgIH0KICAgIHByaW50ZiggIlxuIiApOwogCiAgICBzaXplX3QgdG90YWxTaXplID0gdGVzdC5teUR5bmFtaWNBcnJheTEuc2l6ZSArIHRlc3QubXlEeW5hbWljQXJyYXkyLnNpemU7CiAgICB1aW50OF90ICogYWxsRGF0YSA9IG5ldyAobm90aHJvdykgdWludDhfdFsgdG90YWxTaXplIF07CiAgICBpZiAoIGFsbERhdGEgIT0gbnVsbHB0ciApCiAgICB7CiAgICAgICAgbWVtY3B5KCBhbGxEYXRhLCB0ZXN0Lm15RHluYW1pY0FycmF5MS5kYXRhLCB0ZXN0Lm15RHluYW1pY0FycmF5MS5zaXplICk7CiAgICAgICAgbWVtY3B5KCBhbGxEYXRhICsgdGVzdC5teUR5bmFtaWNBcnJheTEuc2l6ZSwgdGVzdC5teUR5bmFtaWNBcnJheTIuZGF0YSwgdGVzdC5teUR5bmFtaWNBcnJheTIuc2l6ZSApOwogCiAgICAgICAgZm9yICggc2l6ZV90IGkgPSAwOyBpIDwgdG90YWxTaXplOyBpKysgKQogICAgICAgIHsKICAgICAgICAgICAgcHJpbnRmKCJhbGxEYXRhWyVkXSA9ICVkXG4iLCBpLCBhbGxEYXRhW2ldICk7CiAgICAgICAgfQogCiAgICAgICAgZGVsZXRlW10gYWxsRGF0YTsKICAgIH0KIAogICAgZGVsZXRlW10gdGVzdC5teUR5bmFtaWNBcnJheTEuZGF0YTsKICAgIGRlbGV0ZVtdIHRlc3QubXlEeW5hbWljQXJyYXkyLmRhdGE7CiAKICAgIHJldHVybiAwOwp9