title: C: Pipe: How to pass a dynamic int array using pipe correctly
body: <p>I tried to write a dynamic allocated int array in several child processes, and read them in parent process using pipe, but the result is like [-15236548, 37526, -15236548, 37526,0,0,0,0,0,0], it looks like the addresses are read instead of the real values[1,1,1,1,0,2,1,1,1,1]. I am really struggling in it for the whole day, any help would be greatly appreciated!!!</p>
  
  <p>The child process are 10, they would first write an array each, and after all of them are done, parent process reads the 10 array. </p>
  
  <pre><code>int *getArr(){
      int * arr1 = malloc(sizeof(int) *100);
      //do something
      return arr1;
  }
  
  //write values to pipe
  void write_check(int fd, void *buffer, size_t len){
      char *p = buffer;
      while(len&gt;0){
          size_t wlen = write(fd, p, len);
          if(wlen &lt;= 0)
              exit(0);
          p += wlen;
          len -= wlen;
      }
  }
  
  // read values from pipe
  void read_check(int fd, void *buffer, size_t len){
      char *p = buffer;
      while(len&gt;0){
          size_t rlen = read(fd, p, len);
          if(rlen &lt;= 0)
              exit(0);
          p += rlen;
          len -= rlen;
      }
  }
  
  // pass values, and get it ready to be wrote
  void write_ints(int fd, int *p, int len){
      write_check(fd, p, len * sizeof(*p));
  }
  
  // allocate memory for fetching values
  void read_ints(int fd, int *p, int len){
      p = malloc(len * sizeof(*p));
      read_check(fd, p, len* sizeof(*p));
  }
  
  
  int main(){
      int len = 100;
      int *arr1 = malloc(sizeof(int) * 100);
      int *fds = malloc(sizeof(int) * 10 * 2);
  
      // child processes write values to pipe
      for(int i=0; i&lt;10; i++){
          child_pid = fork();
          if (child_pid == 0){
              // do something
              arr1 = getArr();
              write_ints(fd[i*2+1], &amp;arr1, len);
          }
      }
      // parent process reads values 
      for(int i=0; i&lt;10; i++){
          // do something
          read_ints(fd[i*2], &amp;arr1, len);
      }
  }
  </code></pre>