Memory allocation

This is a discussion on Memory allocation within the C Programming forums, part of the General Programming Boards category; Is there a way to allocate 1 000 000 000 array of integers ? I try d using dynamic memory ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    11

    Memory allocation

    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.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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 05:31 AM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    Yes well i actually need the space for memorizing a lot of numbers.
    See my code :

    Code:
    #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;
    }
    and a really don't know any other way to do this

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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:
    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];
    }
    You would calculate the value each time, but at least the values up to MAXTABLE are quick to get.

    --
    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.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    Thanks especially you matsp that helped me a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 06:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 04:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21