This piece of code causes segmentation fault:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SLIDINGWINDOW 5
#define SEGMENTSIZE 100
int main() {
char** send_buffer = (char**) malloc (SLIDINGWINDOW);
int i;
for (i = 0; i<SLIDINGWINDOW; ++i) {
send_buffer[i] = (char*) malloc (SEGMENTSIZE);
}
for (i = 0; i<SLIDINGWINDOW; ++i) {
strcpy(send_buffer[i], "Hello, world");
printf("%s\n", send_buffer[i]);
fflush(NULL);
}
}
The strange thing is if you put the content of the second loop into the first loop, it works!
Could anyone see why this happens? Many thanks!