I'm trying to do this:
int main(void){
u_int64_t NNUM = 2<<19;
u_int64_t list[NNUM], i;
for(i = 0; i < 4; i++){
list[i] = 999;
}
}
Why am I getting segfault at my Ubuntu 10.10 64 bits (gcc 4.6.1)?
I'm trying to do this:
int main(void){
u_int64_t NNUM = 2<<19;
u_int64_t list[NNUM], i;
for(i = 0; i < 4; i++){
list[i] = 999;
}
}
Why am I getting segfault at my Ubuntu 10.10 64 bits (gcc 4.6.1)?
You try to create a very large array on the stack. This leads to a stack overflow.
Try allocating the array on the heap instead. For example:
// Allocate memory
u_int64_t *list = malloc(NNUM * sizeof(u_int64_t));
// work with `list`
// ...
// Free memory again
free(list);
You declare NNUM = 2*2^19 == 2<<19 == 1048576
.
and try to allocate on the stack 64 bits * 1048576 = num of bits* num of cells.
It is 8.5 MegaBytes
, it is just too much for allocation on the stack, you can try to allocate it on the heap and check if it really works using the return value of malloc
.
Your program requires a minimum stack size of 1048576, if you check with 'ulimit -s', it is most likely less than that. you can try 'ulimit -s 16384' and then re-execute again.