1

Given Below is the code which stores random integers in a binary file. The size of the binary file should be 65536 bytes, so I have to generate total 16634 integers and then save them 16 integers at a time. This is has been implemented in populateBackingStore() function. The Problem here is that a segmentation fault occurs "sometimes" at line:

buffer [i%16] = rand ()  % MAX_INT;.

Note: For debugging purpose I have printed the iteration number and I have found that the segmentation fault only occurs at 16358th iteration.

#include <stdio.h>

#define     BACKING_STORE   65536
#define     MAX_INT     42949678295

int populateBackingStore () {
    FILE * backingStore = fopen ("BACKING_STORE.bin", "wb");
    int num, i;
    int * buffer = (int* ) malloc (16);     // 64 * (4 bytes) = 256 bytes 

    if (backingStore == NULL) {
        printf ("Error while creating file BACKING_STORE.bin\n");
        return NULL;
    }

    srand (time (NULL));
    for (i = 0; i < BACKING_STORE/sizeof (int) ;i ++) {
        buffer [i%16] = rand ()  % MAX_INT;
        if ( i % 16 == 0 ) {
            fwrite (buffer, sizeof (int), 16, backingStore);
        }           
        printf ("%d ", i);
    }
    fclose (backingStore);  
}

int main () {
    populateBackingStore ();

    return 0;
}
  • 1
    First read more about [`malloc`](http://en.cppreference.com/w/c/memory/malloc), then [don't cast `malloc` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Dec 20 '14 at 12:51
  • Yes, I have checked the content of buffer, it is working as it should be. Just like I said before, this code causes errors "occasionally", other times It works perfectly fine. – Ahmed Suffian Javed Dec 20 '14 at 12:53
  • Also, there's already an [`UINT_MAX` defined in the standard library](http://en.cppreference.com/w/c/types/limits). – Some programmer dude Dec 20 '14 at 12:53
  • `causes errors "occasionally"`...that's the output of _undefined behaviour_ when you access out-of-bound memory. – Sourav Ghosh Dec 20 '14 at 12:54
  • As for the occasional part, that's part of the [*undefined behavior*](http://en.wikipedia.org/wiki/Undefined_behavior) you're having. – Some programmer dude Dec 20 '14 at 12:54

1 Answers1

7

int * buffer = (int* ) malloc (16);

You're not allocating enough space for 16 ints. You're allocating 16 bytes.

Change to

int * buffer = malloc (16 * sizeof (*buffer));

Related reading :

  1. malloc() man page.
  2. Do not cast the return value of malloc().
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • out of curiosity, Why was this segmentation fault being caused occasionally? As in this error should have occurred every single time I had to run this program, but that was not the case? – Ahmed Suffian Javed Dec 20 '14 at 13:02
  • @AhmedSuffianJaved yeah, that is called [_undefined behaviour_](https://en.wikipedia.org/wiki/Undefined_behavior). You cannot _predict_ [ref : `every single time`] what's the outcome is going to be. IMHO, if your program gets segfault, consider yourself _lucky_ . Otherwise, you may end up with a completely wrong answer and you won't have a clue about that. – Sourav Ghosh Dec 20 '14 at 13:05