Why does this give a segmentation fault?

Posted by nightcracker on Stack Overflow See other posts from Stack Overflow or by nightcracker
Published on 2011-01-11T23:47:22Z Indexed on 2011/01/11 23:53 UTC
Read the original article Hit count: 265

Filed under:
|
|

I'm stunned, why does this code give me a segmentation fault?

#include <stdio.h>

#define LIMIT 1500000

typedef struct {
    int p;
    int a;
    int b;
} triplet;

int main(int argc, char **argv) { 
    int i;
    triplet triplets[LIMIT];

    for (i = 0; i < 5; i++) {
        triplets[i].p = 9; // remove this line and everything works fine
    }

    printf("%d\n", triplets[15].p);

    return 0; 
}

EDIT: After changing LIMIT to 150 I no longer get a segmentation fault, it prints random numbers instead.

© Stack Overflow or respective owner

Related posts about c

    Related posts about struct