1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

help me in a big C program

Discussion in 'C' started by techme, Jun 1, 2010.

  1. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Hi friends i wrote a C program for solving Poisson equation over a square plate.Where we have to define the grid size.i have defined several 2-d arrays in program to store the values. the problem size is quite big so i have to define the size of array
    of order of 10000x10000...
    but when i compile the program. it shows some error. how can i use such big arrays without much trouble.
     
  2. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    hey buddy!
    don't try to declare this type...
    your compiler will show errors...
    :(
     
  3. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    The size of each integer is 4bytes under windows and the C Compiler generally allocates memory in RAM. So , for now ur allocated memory is 40,00000 bytes and you do not have that much size of RAM installed in ur CPU. As a result , ur PC show error or does not responds.
     
  4. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    using tc its 2bytes for integers and in gcc its 4 bytes but it would be better if you use dynamic allocation for doing it so.
     
  5. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    I would rather go with a dynamically allocated array, that is, instead of

    double array[MAX][MAX];

    do something like
    Code:
    double *array[MAX];
    int i;
    for(i = 0; i < MAX; i++) {
    array[i] = (double*) malloc(MAX * sizeof(double));
    /* Insert here some check if the malloc went OK, be prepared for an IMPROBABLE out of memory situation */
    }
    Now you can use array[j] exactly the same way as before, it just works a bit different way internally: the program looks at array, which is a pointer on double, so it can be used as an array of doubles, and then addresses the j-th element thereof.

    You must not forget to deallocate all this memory at the end of your program, though:

    Code:
    for(i = 0; i < MAX; i++) {
    free(array[i]);
    }
    Hope this helps! [If not, you can try copying the error message here so that we can tell how to fix it.]

    PS: You should not be limited by your computer memory too much. A 10000 x 10000 array of doubles still takes less than 1 GB. And virtually every computer nowadays uses virtual memory which is swapped on your hard drive. Rest assured that many other programs use MUCH more memory and work well.
     

Share This Page