1

Hi guys I tested c++ dynamic array and I changed 2 inputs every time. but array size is not changed. Why?

#include <iostream>

using namespace std;

int main()
{
int r = 0;
int c = 0;
int cnt = 1;

cin >> r;
cin >> c;

int** arr1 = new int* [r];


for (int i = 0; i < r; i++)
{
    arr1[i] = new int[c];
}

cout << sizeof(arr1) << endl;
cout << sizeof(arr1[0]);
}

I knew that If I entered two value 3 and 4 then results are 3 and 4

but the results are 4 and 4

Hamp
  • 39
  • 5

1 Answers1

2

You are testing the size of a pointer which is always 4 on 32 bit. That's because sizeof is a compile time thing; it cannot determine an array size.

Generally arrays do not carry size information, that is why functions like strlen need a null terminator.

Suggestion: use std::vector.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
  • To add to this answer, you should debug your program put a break point after your for loop. Modern IDEs will let you see each variable being used and its values/e,element. While vector is a great option, because you have the sizes input, just keep those handy whenever you need the size of the array – arc-menace Jun 28 '20 at 20:33
  • @Michael Chourdakis Thank you Thank you so much – Hamp Jun 29 '20 at 02:47