size of a dynamically created array

I have declared an array like:
/***********Creating an m*p array**********************/
	 B  = new int *[m];
       B[row] = new int[p];

How to find the number of elements in it?

The statement
cout << "number of elements in array B equals " << sizeof(B) << endl;
returns 4 each time the program runs
As far as I know when creating elements dynamically like that there is no way to find out the number of elements (as it's just a pointer) so sizeof wont work. Either keep track of the number yourself (in this case 'm') or use vectors.
Obviously, B being a m_lines x p_columns (2D) array, the number of its elements is simply the product...m*p. Each of m lines has p elements...That's all!
Last edited on
function sizeof() will always return the size of data type, not the size of block pointed by pointer.
Topic archived. No new replies allowed.