Take the 2-minute tour ×  This piece of code causes segmentation
fault:  #include <stdio.h> #include <stdlib.h> #include
<string.h> #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);          strcpy(send_buffer[i],
"Hello, world");         printf("%s\n", send_buffer[i]);  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!  share|improve this question add comment  2 Answers
up vote 6 down vote accepted  The size passed to malloc() isn't
correct. You probably meant:  char** send_buffer = malloc
(SLIDINGWINDOW * sizeof(send_buffer[0]));   send_buffer[i] =
malloc (SEGMENTSIZE * sizeof(send_buffer[i][0]));  This is
because the argument to malloc() is the number of bytes you're
asking for, so you need to multiply the length you want by the
size of the elements.  In addition,