jamesjuett / lobster

Interactive Program Visualization Tools
8 stars 3 forks source link

Red text for invalid pointer in place it shouldn't be #255

Closed jamesjuett closed 2 years ago

jamesjuett commented 3 years ago

Repro below. The red text on the addresses being passed in for the function args.

#include <iostream>
#include <vector>
using namespace std;

bool are_mirrored(const int arr1[], const int arr2[], int n) {
//     if (!*arr1 || !*arr2) { 
//      if (!*arr1 && !*arr2) {return true;}
//       else {return false;}
//     }

     arr2 = arr2 +  n - 1;
    for (int i = 0; i < n; ++i) {
    if (*arr1 != *arr2) {return false;}
      ++arr1;
      --arr2; 
    }
    return true; 
}

int main() {
  int arr1[3] = {1, 2, 3};
  int arr2[3] = {3, 2, 1};
  assert(are_mirrored(arr1, arr2, 3)); // true

  int arr3[3] = {1, 2, 3};
  int arr4[5] = {5, 4, 3, 2, 1};
  // NOTE: we are only looking at the last 3 
  //       elements of arr2 as a subarray,
  //       so from the perspective of are_mirrored,
  //       it is operating on two arrays of size 3
  assert(are_mirrored(arr3, arr4+2, 3)); // true

  int arr5[3] = {5, 7, 5};
  int arr6[3] = {5, 8, 5};
  assert(!are_mirrored(arr5, arr6, 2)); // false

  int arr7[1] = {2};
  assert(are_mirrored(arr7, arr7, 1)); // true
  assert(are_mirrored(arr7, arr7, 0)); // true
}
jamesjuett commented 3 years ago

Specifically, the issue is that ArrayAggregateInitializer doesn't begin the lifetime of the array object.