fork download
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. class myClass
  6. {
  7. public :
  8.  
  9. myClass();
  10. ~myClass();
  11.  
  12. struct DynamicArray
  13. {
  14. uint8_t * data;
  15. size_t size;
  16. };
  17.  
  18. void addToArray( DynamicArray & array, uint8_t * data, size_t len );
  19.  
  20. DynamicArray myDynamicArray1;
  21. DynamicArray myDynamicArray2;
  22. };
  23.  
  24. myClass::myClass() :
  25. myDynamicArray1( { nullptr, 0 } ),
  26. myDynamicArray2( { nullptr, 0 } )
  27. {
  28. }
  29.  
  30. myClass::~myClass()
  31. {
  32. if ( myDynamicArray1.data != nullptr )
  33. {
  34. delete[] myDynamicArray1.data;
  35. }
  36. if ( myDynamicArray2.data != nullptr )
  37. {
  38. delete[] myDynamicArray2.data;
  39. }
  40. }
  41.  
  42. void myClass::addToArray( DynamicArray & array, uint8_t * data, size_t len )
  43. {
  44. DynamicArray tmp;
  45. tmp.size = array.size + len;
  46. tmp.data = new (nothrow) uint8_t[ tmp.size ];
  47.  
  48. if ( tmp.data != nullptr )
  49. {
  50. if ( array.data != nullptr )
  51. {
  52. memcpy( tmp.data, array.data, array.size );
  53. delete[] array.data;
  54. }
  55. memcpy( tmp.data + array.size, data, len );
  56. array.data = tmp.data;
  57. array.size = tmp.size;
  58. }
  59. else
  60. {
  61. printf("sorry, couldn't add to array, memory allocation failed!\n");
  62. }
  63. }
  64.  
  65.  
  66.  
  67. myClass test;
  68.  
  69. int main()
  70. {
  71. uint8_t a[] = { 10, 20, 30, 40, 50, 60, 70 };
  72. uint8_t b[] = { 80, 90, 100 };
  73.  
  74. test.addToArray( test.myDynamicArray1, a, 7 );
  75. test.addToArray( test.myDynamicArray2, b, 2 );
  76. test.addToArray( test.myDynamicArray1, b, 3 );
  77. test.addToArray( test.myDynamicArray2, a, 5 );
  78.  
  79. for ( size_t i = 0; i < test.myDynamicArray1.size; i++ )
  80. {
  81. printf( "test.myDynamicArray1.data[%d] = %d\n", i, test.myDynamicArray1.data[i] );
  82. }
  83. printf( "\n" );
  84. for ( size_t i = 0; i < test.myDynamicArray2.size; i++ )
  85. {
  86. printf( "test.myDynamicArray2.data[%d] = %d\n", i, test.myDynamicArray2.data[i] );
  87. }
  88. printf( "\n" );
  89.  
  90. size_t totalSize = test.myDynamicArray1.size + test.myDynamicArray2.size;
  91. uint8_t * allData = new (nothrow) uint8_t[ totalSize ];
  92. if ( allData != nullptr )
  93. {
  94. memcpy( allData, test.myDynamicArray1.data, test.myDynamicArray1.size );
  95. memcpy( allData + test.myDynamicArray1.size, test.myDynamicArray2.data, test.myDynamicArray2.size );
  96.  
  97. for ( size_t i = 0; i < totalSize; i++ )
  98. {
  99. printf("allData[%d] = %d\n", i, allData[i] );
  100. }
  101.  
  102. delete[] allData;
  103. }
  104.  
  105. delete[] test.myDynamicArray1.data;
  106. delete[] test.myDynamicArray2.data;
  107.  
  108. return 0;
  109. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
test.myDynamicArray1.data[0] = 10
test.myDynamicArray1.data[1] = 20
test.myDynamicArray1.data[2] = 30
test.myDynamicArray1.data[3] = 40
test.myDynamicArray1.data[4] = 50
test.myDynamicArray1.data[5] = 60
test.myDynamicArray1.data[6] = 70
test.myDynamicArray1.data[7] = 80
test.myDynamicArray1.data[8] = 90
test.myDynamicArray1.data[9] = 100

test.myDynamicArray2.data[0] = 80
test.myDynamicArray2.data[1] = 90
test.myDynamicArray2.data[2] = 10
test.myDynamicArray2.data[3] = 20
test.myDynamicArray2.data[4] = 30
test.myDynamicArray2.data[5] = 40
test.myDynamicArray2.data[6] = 50

allData[0] = 10
allData[1] = 20
allData[2] = 30
allData[3] = 40
allData[4] = 50
allData[5] = 60
allData[6] = 70
allData[7] = 80
allData[8] = 90
allData[9] = 100
allData[10] = 80
allData[11] = 90
allData[12] = 10
allData[13] = 20
allData[14] = 30
allData[15] = 40
allData[16] = 50