Click to See Complete Forum and Search --> : How to get any big array
Yoh-hei
June 8th, 1999, 04:37 AM
Hello:
I have a array like:int MyArray[n][m];
I can't know the value of 'n' and 'm'.They're
must had run the program,get the two value.
I want use point but there was a question.
I need help!
Welcome any advice!
Thank you.
ric
June 8th, 1999, 05:03 AM
Try this:
int **MyArray;
Somewhere in the program:
for(int i(0);i<n;i++)
MyArray(i) = new int (m);
Dont forget to delete your memory,
ric
Roger Allen
June 8th, 1999, 05:06 AM
For a dynamic array at run time, try this :
// to allocate (assume type int for array)
int *pArray ;
pArray = new int[x_array_size * y_array_size] ;
// to access using indexes i for x and j for y
value = pArray[i + j * y_array_size] ;
// to delete array
delete []pArray ;
You will have to deal with making sure that the values of i,j do not go outside the array bounds that you allocate.
HTH
Roger Allen
Oliver Kinne
June 8th, 1999, 06:52 AM
I suppose you've noticed the mistake? To get a m*n array do the following:
int **MyArray;
MyArray = new int*[m];
for (int x = 0; x < m; i++)
{
MyArray[x] = new int[n];
}
int value_at_x_y = MyArray[x][y];
Then, don't forget to delete everything:
for (int x = 0; x < m; x++)
{
delete MyArray[x];
}
delete MyArray;
Tada,
Oliver.
ric
June 8th, 1999, 07:46 AM
OK, you are right, but I run my code on a Borland 3.1 compiler and it doesnot spit out any "Null Pointer
Assignment" so I supposed that it works.
Of course, we must alloc first for the pointers then for ints.
Thanks
Roger Allen
June 8th, 1999, 07:50 AM
A bug!
The line
delete MyArray ;
// should read
delete []MyArray ;
I thought I taught you better than that Oli!
(ps I work with him)
Roger Allen
Yoh-hei
June 8th, 1999, 09:06 PM
Hello:
Thank you very much.
I receive your message.Now My Array like this:
MyArray[m][8];
In fact the value of 'm',I can't get when
the program start run.The value can get when
open file message is selected.(I use VC++6.0 MFC)
Welcome any advice!
Thank you very much.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.