Length of Dynamically Created Multidemensional Arrays

G

GraemeC

How can you determine the length of a dynamically created
multidimensional array?

Tried using sizeof unsuccesufully. It always returns 4, presumably
becuase I am using ints and it is just telling me the size of the
pointer myArray

Many thanks

G


int ** myArray = 0;
int Rows;
int Cols;

cout << "Number of Rows?" << endl;
cin >> Rows;

myArray = new int*[Rows];

for (int i_r = 0; i_r < Rows; i_r++)
{
cout << "Number of cols?" << endl;
cin >> Cols;

myArray[i_r] = new int[Cols];
for (int i_c=0; i_c < Cols; i_c++) {
cout << "Number" << i_c << endl;
cin >> myArray[i_r][i_c];
}
}
}

return 0;
}
 
R

Rolf Magnus

GraemeC said:
How can you determine the length of a dynamically created
multidimensional array?

By remembering the size you specified when creating it. Alternatively, use
std::vector instead of an array.
Tried using sizeof unsuccesufully. It always returns 4, presumably
becuase I am using ints and it is just telling me the size of the
pointer myArray

It has nothing to do with int. If you use sizeof on a pointer, you get the
size of a pointer.
 
P

pmouse

To put it simply: You can't.

Therefore, you either store the length somewhere, or you put a marker
at the boundaries of the array.

look for STL's vector type.

Regards,

PQ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top