title: Writing and reading struct with dynamic array through a pipe in C
body: <p>I have a struct with a dynamic array inside of it:</p>
  
  <pre><code>struct mystruct {
   int count;
   int *arr;
  } mystruct_t;
  </code></pre>
  
  <p>and I want to pass this struct down a pipe in C and around a ring of processes. When I alter the value of count in each process, it is changed correctly. My problem is with the dynamic array.</p>
  
  <p>I am allocating the array as such:</p>
  
  <pre><code>mystruct_t x;
  x.arr = malloc( howManyItemsDoINeedToStore * sizeof( int ) );
  </code></pre>
  
  <p>Each process should read from the pipe, do something to that array, and then write it to another pipe. The ring is set up correctly; there's no problem there. My problem is that all of the processes, except the first one, are not getting a correct copy of the array. I initialize all of the values to, say, 10 in the first process; however, they all show up as 0 in the subsequent ones.</p>
  
  <pre><code>for( j = 0; j &lt; howManyItemsDoINeedToStore; j++ ){
      x.arr[j] = 10;
  }
  </code></pre>
  
  <p>Log:</p>
  
  <pre><code>Initally:       10      10      10      10      10
  After Proc 1:   9       10      10      10      15
  After Proc 2:   0       0       0       0       0
  After Proc 3:   0       0       0       0       0
  After Proc 4:   0       0       0       0       0
  After Proc 5:   0       0       0       0       0
  After Proc 1:   9       10      10      10      15
  After Proc 2:   0       0       0       0       0
  After Proc 3:   0       0       0       0       0
  After Proc 4:   0       0       0       0       0
  After Proc 5:   0       0       0       0       0
  </code></pre>
  
  <p>Now, if I alter my code to, say, </p>
  
  <pre><code>struct mystruct {
   int count;
   int arr[10];
  } mystruct_t;
  </code></pre>
  
  <p>everything is passed correctly down the pipe, no problem. I am using <code>read</code> and <code>write</code> in C:</p>
  
  <pre><code>write( STDOUT_FILENO, &amp;x, sizeof( mystruct_t ) );
  read( STDIN_FILENO, &amp;x, sizeof( mystruct_t ) );
  </code></pre>