|
-
June 8th, 1999, 04:37 AM
#1
How to get any big array
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.
-
June 8th, 1999, 05:03 AM
#2
Re: How to get any big array
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
-
June 8th, 1999, 05:06 AM
#3
Re: How to get any big array
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
Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
Please remember to rate useful answers. It lets us know when a question has been answered.
-
June 8th, 1999, 06:52 AM
#4
Re: How to get any big array
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.
-
June 8th, 1999, 07:46 AM
#5
Re: How to get any big array
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
-
June 8th, 1999, 07:50 AM
#6
Re: How to get any big array
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
Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
Please remember to rate useful answers. It lets us know when a question has been answered.
-
June 8th, 1999, 09:06 PM
#7
Re: How to get any big array
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|