Find the answer to your Linux question:
Results 1 to 5 of 5
I want to know the maximum size of Two dimensional Array that can hold float values. I need the follwing declariation of array but it donot work. Please try to ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Location
    Meerut, Up, India
    Posts
    6

    Size of array in C( Segmentation fault occures)

    I want to know the maximum size of Two dimensional Array that can hold float values. I need the follwing declariation of array but it donot work. Please try to help -


    float x[2000][2000];



    Error- Segmentation fault error occures

    Please give me your valuable suggestion regarding this matter.

  2. #2
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    What Compiler do you use?
    With what flags?
    New Users, please read this..
    Google first, then ask..

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Assuming that he's using any stock gcc on an IBM-compatible PC running any Linux, the probability is extremely low that the choice of compiler or compilation flags are the problem.

    The problem is that he's likely declaring this as an array that's local to some C function, rather than global to the whole program. The difference is that the space for functions which are local to a function is found on the stack, which is large enough for most purposes, but will choke when you try to use a very large array. It will choke not when you try to access the array, but when you first call the function, because that is when the stack pointer is altered to accommodate local variables for the function. For example this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      float x[2000][2000];
    
      printf("fred\n");
    
      x[1999][1999]=3.0/5.0;
    
      printf("%f\n",x[1999][1999])
    
      return 0;
    
    }
    will blow up before you even see "fred" output on the screen.

    One solution is to make x a global variable (declared outside any function). Another is to use malloc().

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Just Joined!
    Join Date
    Dec 2007
    Location
    Meerut, Up, India
    Posts
    6

    Size of array in case of multiple declaration of array of higher size in C

    As per your suggestion the following program work

    #include <stdio.h>
    float x[9500][9500];

    int main(void)
    {

    printf("fred\n");

    x[9499][9499]=3.0/5.0;

    printf("%f\n",x[9499][9499]);

    return 0;

    }

    But when we declare more than one two-dimensional array then the error" KILLED" is flashed in the following prgram. Please try to help so that i could declare more than one two dimensional array of float type as it is required in our research program.

    #include <stdio.h>
    float x[9500][9500];
    float y[9500][9500];
    int main(void)
    {

    printf("fred\n");

    x[9499][9499]=3.0/5.0;

    printf("%f\n",x[9499][9499]);

    y[9499][9499]=3.0/5.0;

    printf("%f\n",y[9499][9499]);

    return 0;

    }

    output message-"killed"

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I copied and pasted your new program into my system, and it compiled and ran without error. "Killed" means that the program somehow received a kill signal, and terminated. I'm at a loss as to why that happened.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...