0

I am learning, so it is probably something trivial. The length should be 2 (call down below), but I get 4 in the output. Can someone tell me my mistake?

BigInt::BigInt(const unsigned short int ip[])
  : is_negative(false), length(sizeof(ip) / sizeof(ip[0])), digits(new unsigned short[sizeof(ip) / sizeof(ip[0])]) {
  // assumption: this case is alway is_negative = false (array of short int to BigInt)
  cout << "length is " << length << endl;
  for (int i = 0; i < length; i++) {
    digits[i] = ip[i];
  }
  cout << "made from int arr of length "<< length << "--" << (sizeof(ip) / sizeof(ip[0])) << endl;
}

Called from main:

void test_cmp() {
  cout << "---- ---- ---- cmp val ---- ---- ----" << endl;
  BigInt i1(33290); // default arguments
  //DEBUG(i1);
  BigInt i2(33299);
  unsigned short int ip[] = { 1,2 };
  cout << "unsgined short array size" << sizeof(ip)/sizeof(ip[0]) << endl;
  BigInt i3(ip);
  short cmpval = i1.cmp(i3);
  cout << "cmp val" << cmpval << endl;
}
marie_antoinette
  • 181
  • 2
  • 10
  • 2
    In short: You can't get the size of the array in the constructor or any function for that matter when passed in the way you are showing. So don't use built-in arrays. Especially if you are new to the language, just use `std::vector` instead. Otherwise consider `std::array` or pass the array by reference or `std::span`. For details see the duplicates I linked. – user17732522 Nov 17 '22 at 08:08

0 Answers0