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 ...
- 12-05-2007 #1Just 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.
- 12-05-2007 #2
- 12-06-2007 #3
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:
will blow up before you even see "fred" output on the screen.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; }
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.
- 12-10-2007 #4Just 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"
- 12-11-2007 #5
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.


Reply With Quote