Segmentation fault

This is a discussion on Segmentation fault within the C Programming forums, part of the General Programming Boards category; Hello to all,
I am having a problem with my program. I am receiving an error message "Segmentation fault" when ...

That's really, really big for an array -- especially an array stored on the stack. As mike_g has indicated, if you use malloc or even declare that array globally it might solve your problem. (That is, put that line outside of main(), right before it.)

Personally, I suggest you use malloc(). A simple solution would go like this:

Code:

long filesize;
char *data;
/* open the file input in here somewhere */
fseek(input, 0, SEEK_END); /* set the file pointer to the end of the file */
filesize = ftell(input); /* store the current file position -- i.e., the size of the file in bytes */
/* allocate one byte of memory for every byte in the file */
data = malloc(filesize * sizeof(*data));
/* do stuff with the data array, reading the file etc */
free(data); /* don't forget to clean up after yourself . . . . */

That's not an optimal solution, of course; you don't need enough memory to store every byte in the file, just every alphabetic character. But a better solution would involve realloc() and I don't feel like explaining that here . . . .

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

Unfortunately, 100000000 is about 95 MB--too big for the stack! The stack can hold about 1 MB by default, but varies from OS to OS and settings in some linkers/compilers.
And I really doubt you need all that memory either. You need a dynamic memory approach. Allocate a reasonable size with malloc, keep track of size, if your array exceeds the size, reallocate using realloc and free later.