Back to TILs

C++ realloc_01

Date: 2023-02-07Last modified: 2024-02-26

Table of contents


  int *ptr1 = nullptr;
  int *ptr2 = nullptr;

  auto printPointers = [&](int sz, string title) {
    fmt::print("\n{:14} ptr1|{:14} ptr2|{}\n", static_cast<void *>(ptr1),
               static_cast<void *>(ptr2), title);
    for (int i = 0; i < sz; ++i) {
      fmt::print("{},", ptr2[i]);
    }
  };

  printPointers(0, "Initial null");

  ptr2 = (int *)realloc(ptr1, sizeof(int));
  printPointers(1, "First realloc (non initilized value on memory)");

  // Realloc will copy the previous memory content
  for (auto i = 1; i <= 64; ++i) {
    ptr2 = (int *)realloc(ptr1, i * sizeof(int));
    if (ptr2) {
      ptr1 = ptr2;
      ptr2[i - 1] = (i - 1) % 10;  // initilize the i-th position
      if (i % 4 == 0) {
        printPointers(i, fmt::format("Realloc {} x sizeof(int)", i));
      }
    } else {
      fmt::print("Failure on realloc\n");
      break;
    }
  }

  free(ptr2);
  // the pointer address remains the same
  // the content is destroyed
  printPointers(4, "After free ptr2" );

  ptr1 = ptr2 = nullptr;
  printPointers(0, "After nullptr attribution" );

Possible output


           0x0 ptr1|           0x0 ptr2|Initial null

           0x0 ptr1|0x55a374da2170 ptr2|First realloc (non initilized value on memory)
787311666,
0x55a374da2190 ptr1|0x55a374da2190 ptr2|Realloc 4 x sizeof(int)
0,1,2,3,
0x55a374d98d20 ptr1|0x55a374d98d20 ptr2|Realloc 8 x sizeof(int)
0,1,2,3,4,5,6,7,
0x55a374d85710 ptr1|0x55a374d85710 ptr2|Realloc 12 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,
0x55a374d85710 ptr1|0x55a374d85710 ptr2|Realloc 16 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,
0x55a374da20f0 ptr1|0x55a374da20f0 ptr2|Realloc 20 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
0x55a374da20f0 ptr1|0x55a374da20f0 ptr2|Realloc 24 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,
0x55a374da20f0 ptr1|0x55a374da20f0 ptr2|Realloc 28 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,
0x55a374d97c40 ptr1|0x55a374d97c40 ptr2|Realloc 32 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,
0x55a374d97c40 ptr1|0x55a374d97c40 ptr2|Realloc 36 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,
0x55a374d91e70 ptr1|0x55a374d91e70 ptr2|Realloc 40 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
0x55a374d91e70 ptr1|0x55a374d91e70 ptr2|Realloc 44 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,
0x55a374d91e70 ptr1|0x55a374d91e70 ptr2|Realloc 48 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,
0x55a374d91e70 ptr1|0x55a374d91e70 ptr2|Realloc 52 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,
0x55a374d91e70 ptr1|0x55a374d91e70 ptr2|Realloc 56 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,
0x55a374d91e70 ptr1|0x55a374d91e70 ptr2|Realloc 60 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
0x55a374d91e70 ptr1|0x55a374d91e70 ptr2|Realloc 64 x sizeof(int)
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,
0x55a374d91e70 ptr1|0x55a374d91e70 ptr2|After free ptr2
787362161,21926,-1037480123,923707238,
           0x0 ptr1|           0x0 ptr2|After nullptr attribution

References