fork(2) download
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <regex>
  5.  
  6. struct AAA{
  7. unsigned char* type = nullptr;
  8. unsigned char* str = nullptr;
  9. int xxx;
  10. int yyy;
  11. };
  12. struct BBB{
  13. unsigned char* type = nullptr;
  14. unsigned char* str = nullptr;
  15. int zzz;
  16. };
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20. // 前準備
  21. char aaa_str_temp[9] = {0x41,0x42,0x43,0x31,0x32,0x33,0x44,0x45,0x46};
  22. AAA aaa;
  23. AAA* p_aaa = &aaa;
  24. p_aaa->type = (unsigned char*)malloc(1);
  25. p_aaa->type[0] = 0x01;
  26. p_aaa->str = (unsigned char*)malloc(10);
  27. memcpy(p_aaa->str, aaa_str_temp, 9);
  28. p_aaa->str[9] = 0x00;
  29.  
  30. char bbb_str_temp[9] = {0x55,0x56,0x57,0x34,0x35,0x36,0x58,0x59,0x5a};
  31. BBB bbb;
  32. BBB* p_bbb = &bbb;
  33. p_bbb->type = (unsigned char*)malloc(1);
  34. p_bbb->type[0] = 0x02;
  35. p_bbb->str = (unsigned char*)malloc(10);
  36. memcpy(p_bbb->str, bbb_str_temp, 9);
  37. p_bbb->str[9] = 0x00;
  38.  
  39. // まずは内容確認
  40. printf("変更前AAA %02x %s\n", p_aaa->type[0], p_aaa->str);
  41. printf("変更後BBB %02x %s\n", p_bbb->type[0], p_bbb->str);
  42.  
  43. //--------------------------------------------------------------------------------
  44. // ここから同じ処理をp_aaa, p_bbb, 両方書かないといけない。。
  45. //--------------------------------------------------------------------------------
  46. unsigned char* aaa_str_new = (unsigned char*)realloc(p_aaa->str, 12);
  47. if( aaa_str_new == NULL ){
  48. printf("error\n"); exit(1);
  49. }
  50. // アドレス変わったら置き換え
  51. if( p_aaa->str != aaa_str_new ){
  52. p_aaa->str = aaa_str_new;
  53. }
  54. p_aaa->str[9] = 0x41;
  55. p_aaa->str[10] = 0x41;
  56. p_aaa->str[11] = 0x00;
  57.  
  58. unsigned char* bbb_str_new = (unsigned char*)realloc(p_bbb->str, 12);
  59. if( bbb_str_new == NULL ){
  60. printf("error\n"); exit(1);
  61. }
  62. // アドレス変わったら置き換え
  63. if( p_bbb->str != bbb_str_new ){
  64. p_bbb->str = bbb_str_new;
  65. }
  66. p_bbb->str[9] = 0x41;
  67. p_bbb->str[10] = 0x41;
  68. p_bbb->str[11] = 0x00;
  69. //--------------------------------------------------------------------------------
  70. // ここまで
  71. //--------------------------------------------------------------------------------
  72.  
  73. printf("\n");
  74. printf("変更後AAA %02x %s\n", p_aaa->type[0], p_aaa->str);
  75. printf("変更後BBB %02x %s\n", p_bbb->type[0], p_bbb->str);
  76.  
  77. return(1);
  78. }
  79.  
  80.  
Runtime error #stdin #stdout 0s 4472KB
stdin
Standard input is empty
stdout
変更前AAA 01  ABC123DEF
変更後BBB 02  UVW456XYZ

変更後AAA 01  ABC123DEFAA
変更後BBB 02  UVW456XYZAA