title: FD_SET reading from invalid memory address?
body: <p><strong>**EDIT: I have found a solution.**</strong></p>
  
  <p>I have a strange problem for those who dare to read below:</p>
  
  <p>I'm working on a homework assignment, and need to send messages between processes using UNIX pipes.</p>
  
  <p>My intention with this code is to select() on the provided file descriptor. If something is available to read without blocking, I want to return it. If not, I want to return NULL and continue without blocking.</p>
  
  <p>Here's the code inside of my "getMessage" function, where fd is the file descriptor:</p>
  
  <pre><code>message* getMessage(int fd){
      int messageAvailable = 0;
      struct timeval timeout;
      fd_set fd2;
  
      //If there's a message available, read it; if not, continue on without delay
      timeout.tv_sec = 0;
      timeout.tv_usec = 0;
      FD_ZERO(&amp;fd2);
      FD_SET(fd,&amp;fd2);
      messageAvailable = select(FD_SETSIZE,&amp;fd2,NULL,NULL,&amp;timeout);
      if(messageAvailable){
          int bytesRead = 0;
          message* m;
          m = malloc(sizeof(message));
          //Get the header
          bytesRead = read(fd,m,sizeof(message));
          //If we got the whole message
          if(bytesRead == sizeof(message)){
              return m;
          }else{
              //If a message wasn't generated, free the space we allocated for it
              free(m);
              return NULL;
          }
      }else{
          return NULL;
      }
  }
  </code></pre>
  
  <p>This code is inside of a loop that continues for the duration of the program, and at precisely the same point (the next getMessage() call after one message is successfully transferred) it segfaults. Apparently the FD_SET line is reading from an invalid memory location.</p>
  
  <p>Without posting all of my code, can anyone take a guess at what could be happening that's causing a segfault in this simple macro?</p>
  
  <p>I've posted relevant debugging information below, where line 33 corresponds with the FD_SET line above:</p>
  
  <pre><code>==1330== Invalid read of size 1
  ==1330==    at 0x804E819: getMessage (messages.c:33)
  ==1330==    by 0x8049123: main (messageTest.c:110)
  ==1330==  Address 0xde88d627 is not stack'd, malloc'd or (recently) free'd
  ==1330== 
  ==1330== 
  ==1330== Process terminating with default action of signal 11 (SIGSEGV)
  ==1330==  Access not within mapped region at address 0xDE88D627
  ==1330==    at 0x804E819: getMessage (messages.c:33)
  ==1330==    by 0x8049123: main (messageTest.c:110)
  ==1330==  If you believe this happened as a result of a stack
  ==1330==  overflow in your program's main thread (unlikely but
  ==1330==  possible), you can try to increase the size of the
  ==1330==  main thread stack using the --main-stacksize= flag.
  ==1330==  The main thread stack size used in this run was 8388608.
  ==1330== 
  ==1330== HEAP SUMMARY:
  ==1330==     in use at exit: 344 bytes in 10 blocks
  ==1330==   total heap usage: 25 allocs, 15 frees, 2,492 bytes allocated
  ==1330== 
  ==1330== LEAK SUMMARY:
  ==1330==    definitely lost: 12 bytes in 1 blocks
  ==1330==    indirectly lost: 0 bytes in 0 blocks
  ==1330==      possibly lost: 0 bytes in 0 blocks
  ==1330==    still reachable: 332 bytes in 9 blocks
  ==1330==         suppressed: 0 bytes in 0 blocks
  ==1330== Rerun with --leak-check=full to see details of leaked memory
  ==1330== 
  ==1330== For counts of detected and suppressed errors, rerun with: -v
  ==1330== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 11 from 6)
  Segmentation fault
  </code></pre>