Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segmentation fault (core dumped)

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]);
}
like image 692
Percy Avatar asked Feb 14 '12 04:02

Percy


1 Answers

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.

like image 109
Jesus Ramos Avatar answered Nov 15 '22 21:11

Jesus Ramos