C/large array
Expert: Joydeep Bhattacharya - 4/24/2007
QuestionQUESTION: Hello.
I was wondering if you would be able to help me. I'm writing a C program that has to operate with a huge matrix that has dimensions of 2000 x 3000. When I declare this array (int myarray[2000][3000];) I get an error message that array is too large. What should I do? Is it possible to allocate more memory for my array. Unfortunately writing to/reading from file is not an option here because all the operations must take place in the memory (for acceptable speed). Thanks for your help. Sincerely, Tom.
ANSWER: Hi Tom
There is a solution to your problem you have to create the array dynamically using a pointer to a pointer variable and not calls to calloc or malloc can create your array, you have to create the same using farcalloc or farmalloc or by using the interrupt 0x67 for creating the same in the expanded memory.
regards
Joydeep
---------- FOLLOW-UP ----------
QUESTION: Thank you very much for your answer. Unfortunately I did not fully understand it because I am a beginner. Would you be kind enough to send me a short example of how that's done. Thankfully, Tom
AnswerHi Tom
Basically malloc and calloc takes the variable as unsigned int thats the reason they fail to allocate memory for over 64K where as farmalloc takes it as unsigned long try this for int array
int far *ptr;
if(ptr = (int far *)farmalloc(row*col*sizeof(int)))
printf("Memory Allocation Successful");
/*Use your operation here*/
farfree(ptr);
check this out and if you like you can check this site and can join too its my personal website
http://www.scodz.com mainly on C and C++ Programming.
Please feel free to ask in case if you find any difficulty.
regards
Joydeep