Thread: handle large array size 1000000000

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    23

    handle large array size 1000000000

    here it showing segmentation fault....how can i allocate 10^9 memory location
    Code:
    #include<cmath>
    #include<stdlib.h>
    #include<stdio.h>
    //#define MAXSIZE 1000000000
    int flag=0;
    int flag1=0;
    void prime(int n);
    void pow2(int n);
    int main()
    {
    long  long int n,k,g,sum=0,h;
    //int MAXSIZE=100000000;
    //long long int f[100000000];
     int *f;
    f=( int*)malloc(sizeof(  int)*1000000000);
    //printf("entre the no");
    f[0]=0;
    scanf("%lld",&n);
    //k=n;
    for(g=1;g<=n;g++)
    {
    k=g;
    if(g==1 || g==2)
    {
    f[g]=1;
    //printf("f[g]=%d\n",f[g]);
    }
    else{
    pow2(g);
    if(flag1==1){
    //printf("pow\n");
    f[g]=1;
    }
    else if(k%2==1)
    f[g]=g;
    else{
    while(k%2!=1)
    {
    k=k/2;
    }
    f[g]=k;
    }
    //printf("f[g]=%d\n",f[g]);
    }
    }
    for(h=1;h<=n;h++)
    {
    sum=sum+f[h];
    }
    printf("%lld\n",sum);
    return(0);
    }
    /*void prime(int n)
    {
    int i,z;
    for (i=2;i < n;i++)
    {
    if(n%i==0)
    z=1;
    }
    if(z== 1)
    flag=1;
    //printf("not prime \n");
    else
    flag=0;
    //printf("prime\n");
    }*/
    void  pow2(int n)
    {
    int d=0,p=0,m;
    m=n;
    while(n/2!=0)
    {
    n=n/2;
    d++;
    }
    
    
    p=pow(2,d);
    
    if(p==m)
    flag1=1;
    else
    flag1=0;
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    That's about 4GiB.

    If you want to allocate that much you'll need a 64-bit "OS" or a 32-bit "OS" with "PAE".

    You also may need to configure the "OS" to let a single application own that much memory.

    Beyond that, there are far better algorithms to deal with prime numbers.

    Soma

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    23
    its nt the code of prime number
    Quote Originally Posted by phantomotap View Post
    O_o

    That's about 4GiB.

    If you want to allocate that much you'll need a 64-bit "OS" or a 32-bit "OS" with "PAE".

    You also may need to configure the "OS" to let a single application own that much memory.

    Beyond that, there are far better algorithms to deal with prime numbers.

    Soma

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    If you're lucky your system has 4 GB (4 x 10^9) bytes. I doubt any system can give you that many integers - since you're not the only one using the memory.
    You need to rethink your requirements - perhaps consider arrays that are swapped to temporary files.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Use a disk based approach.

    1000000000 ints (32-bits ints) occupy almost 4G in memory.

    Also, the header <cmath> is unrecognized by my compiler;
    you don't need to cast the return value of malloc;
    and use better indentation in the future.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    as phantomap says, you need a 64 bit OS and compile for 64 bits. on windows 7 64, with Visual Studio 2012,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    	unsigned char *p;
    
    	p = malloc(sizeof(int) * 1000000000);
    
    	printf("%p\n",p);
    
        return 0;
    }
    outputs 000000013F730070
    with a 32 bit build, it outputs 00000000.

    and you don't necessarily need that much physical RAM, as the virtual memory system will swap pages as required. Although if you have TOO little RAM, it will thrash and run very slow.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > sum=sum+f[h];
    If you put this inside the other loop, you wouldn't need to store a huge array at all.

    Also, indentation is something you need to work on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3D Array Size Too Large?
    By krissycokl in forum C Programming
    Replies: 6
    Last Post: 03-08-2012, 11:03 AM
  2. Creating a dynamic array of very large size
    By will of fortune in forum C Programming
    Replies: 9
    Last Post: 04-04-2011, 02:41 PM
  3. Large Array Size
    By smudge256 in forum C Programming
    Replies: 9
    Last Post: 03-10-2010, 10:01 PM
  4. how to get and use large size array.
    By lehe in forum C++ Programming
    Replies: 7
    Last Post: 03-13-2009, 09:13 AM
  5. Finding Words in a array[size][size]
    By ^DJ_Link^ in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 03:51 PM