3

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)?

0x90
  • 39,472
  • 36
  • 165
  • 245
Frederico Schardong
  • 1,946
  • 6
  • 38
  • 62

3 Answers3

5

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);
sth
  • 222,467
  • 53
  • 283
  • 367
2

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.

heap VS. stack

0x90
  • 39,472
  • 36
  • 165
  • 245
0

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.

pizza
  • 7,296
  • 1
  • 25
  • 22