How can make int val[100000000] array?
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main() {
int val[100000000] ;
printf("%d", CHAR_BIT);
}
When I execute the开发者_StackOverflow中文版 code, It occur "segment fault" error. I suppose this error mean that there is no enough memory on the heap area.
No, it's not the heap that's the problem, it's the stack. You've declared a local variable, and local variables live on the stack.
Try int *val = malloc(sizeof(int)*100000000);
instead. You can test whether that succeeds by examining whether (val != NULL)
.
(Of course, you'll need to remember to call free(val)
when you're done with the memory.)
Arrays are allocated on the stack. Try using the heap with malloc
instead.
By default, the stack size of a thread is probably about 2 megabytes. You've tried to allocate more than that on the stack, which causes an error.
Depending on your platform, you may be able to change the default stack size allocation with the --stack
option to ld
, but that's not recommended. It's preferable in your case to use malloc()
. The stack should not normally be used to store large data structures.
精彩评论