Segmentation fault (core dumped)

Percy picture Percy · Feb 14, 2012 · Viewed 32.2k times · Source

I am relatively new on C, I am trying to run a simple program and I get this Error message: Segmentation fault (core dumped) I just want to print any value of the array bits but I can not, I'd appreciate any help on this error.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>

int main()
{
    const long int N = 1000000000;
    const int smallN = 125000000;
    char bits[smallN];

    for(int i=0; i<smallN; i++){
        bits[i]=0xff;
    }

    printf("character = %c \n", bits[5]);
}

Answer

Jesus Ramos picture Jesus Ramos · Feb 14, 2012

The array seems to be exceeding the stack size (bits is an array on the stack). You can either try making it global or allocating the array using malloc.