Is there a way to allocate 1 000 000 000 array of integers ?
I try d using dynamic memory , malloc and calloc but i can' get anything bigger than 500000000.
Any suggestion would be appreciated.
Is there a way to allocate 1 000 000 000 array of integers ?
I try d using dynamic memory , malloc and calloc but i can' get anything bigger than 500000000.
Any suggestion would be appreciated.
That's because, assuming an integer is 4 bytes.
4 * 1, 000, 000, 000 = 4,000,000,000 bytes or ~3815mb
That's a huge amount of memory, where do you expect it to come from? I'd suggest rethinking why you need almost 4GB of memory to store some numbers.
Last edited by zacs7; 10-11-2007 at 04:31 AM.
Am I right in thinking that the machine this is on is a 32-bit Windows system?
If you want to be able to allocate (almost) 4GB, you need to have an address space that is bigger than 4GB [or an OS that uses very little memory - and considering that even the PCI addresses cover some 300-800MB in a modern PC, it's unlikely that you'll get there with a 32-bit OS]. (( If this makes no sense at all to you, don't worry - just accept that a 32-bit OS won't do it for you )).
As zacs7 said, you may want to reconsider your approach to storing data. Why do you think you need to do it this way?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Yes well i actually need the space for memorizing a lot of numbers.
See my code :
and a really don't know any other way to do thisCode:#include<stdio.h> #include<math.h> #include<stdlib.h> int *tabela;//or maybe a static array int max(int a,int b) { if(a<=b)return b; return a; } int f(int n) { if(n==1)return 1; if(n==2)return 2; if(tabela[n]==0){ tabela[n]=max(n,f(n/2)+f(n/3)+f(n/4)); } return tabela[n]; } int main() { int i,n; tabela= (int *)calloc(500000000 , sizeof (int)); tabela[1]=1; tabela[2]=2; tabela[3]=3; while(scanf("%d",&n)!=0)printf("%d\n",f(n)); free(tabela); return 0; }
From what I can tell you're only really using 4 elements.
> tabela[1]=1;
Array indexes in C are zero based, ie they start at 0 not 1. Why don't you resize the array as you get more inputs, or ask the user how many integers they wish to enter. It's a huge waste of resources to allocate that much memory.
Don't cast malloc and friends, that includes calloc(), also check the return value of calloc() - NULL if it fails. Explain in dot points what you need to do.
Well, there's not much you can do about it - if you change your boot.ini and set the /3GB switch there, you will be able to allocate up to 3GB of memory in your usermode app.
You'll also need to add the /LARGEADDRESSAWARE switch to your build [this is for MS compiler - if you are using a gcc-compiler, I don't know what the switch is].
Next step is to get an OS that allows you more than 4GB address space, meaning a 64-bit OS - Windows XP X64 or Linux x86_64 would allow you to do that - then the limit is HUGE - something like 128 terabytes. [Or was that the limit for the kernel space?]
Finally, if you haven't got 4GB of RAM in your machine, I wouldn't attempt to allocate more memory than you have RAM - you may as well just calculate the number every time.
Actually, one solution is to change your code to something like this:
You would calculate the value each time, but at least the values up to MAXTABLE are quick to get.Code:[B]int f(int n) { if(n==1)return 1; if(n==2)return 2; if(n > MAXTABLE) return max(n,f(n/2)+f(n/3)+f(n/4)); else if (tabela[n]==0){ tabela[n]=max(n,f(n/2)+f(n/3)+f(n/4)); } return tabela[n]; }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thanks especially you matsp that helped me a lot.