title: mmap() return not passed between functions
body: <p>I am sure I am missing something simple but I have been knocking my head against the proverbial wall for a few days and still no closer after reading mmap several times and googling.</p>
  
  <p>I have a (condensed) program here where a shared memory space is used to try and enable debugs/tracing whilst the program is running but I am at the first stage of trying to return a mmap pointer back to the main routine without worrying about the caller yet.</p>
  
  <p>The main routine calls the POSIX shared memory functions in another routine (sharedmemoryv.c) and that seems to have successful returns for shm_open, ftruncate and mmap, however when control is reverted back to the main routine, the return for mmap is NULL (*debug_flag) resulting in a SEGV error.</p>
  
  <p>Any nudge or guidance would be greatly appreciated.</p>
  
  <p>Output of running the application is as follows:</p>
  
  <pre><code>./STACKOVERFLOW.o 
  PError is: : Success
  IN SHARED MEMORY FNT1 0
  IN SHARED MEMORY FNT2 1
  debug flag is null
  Segmentation fault
  </code></pre>
  
  <p>GDB shows that it is indeed related to the debug_flag not having anything assigned to it when previously it did.</p>
  
  <pre><code>debug flag is null
  
  Program received signal SIGSEGV, Segmentation fault.
  0x0000000000400a4c in print_debug (debug_flag=0x0, title_string=0x400cad "debug prog", first_string=0x400c9e "finishing main", second_string=0x400c9d "") at debug.c:8
  8       if(DEBUG_ON == *debug_flag)
  </code></pre>
  
  <p>main.c:</p>
  
  <pre><code>     1  
       2  #include &lt;stdio.h&gt;
       3  #include &lt;stdlib.h&gt;
       4  
       5  #include "debug.h"
       6  #include "sharedmemoryv.h"
       7  #include "sharedmemory.h"
       8  
       9  
      10  int *debug_flag = NULL;
      11  
      12  int main(void)
      13  {
      14      enum data_type { TYPE_INT_SIGNED, TYPE_LONG_LONG_SIGNED, TYPE_CHAR_SIGNED };
      15  
      16      enum data_type type_for_debug = TYPE_INT_SIGNED;
      17  
      18      shared_memory_variable(type_for_debug, debug_flag, DEBUG_FLAG_1);   
      19  
      20      if(debug_flag == NULL)
      21      {
      22           printf("%s\n", "debug flag is null");
      23      }
      24      print_debug(debug_flag, "debug prog", "finishing main", BLANK_STRING);
      25  
      26      return 0;
      27  }
  </code></pre>
  
  <p>sharedmemoryv.c: </p>
  
  <pre><code>     1  #include &lt;stdio.h&gt;
       2  #include &lt;sys/mman.h&gt;
       3  #include &lt;sys/stat.h&gt;        
       4  #include &lt;fcntl.h&gt;           
       5  #include &lt;unistd.h&gt;
       6  #include &lt;sys/types.h&gt;
       7  #include &lt;string.h&gt;
       8  #include &lt;stdlib.h&gt;
       9  #include &lt;errno.h&gt;
      10  
      11  #include "debug.h"
      12  
      13  int shared_memory_variable(int type, void *variable, char *shared_memory_reference)
      14  {
      15      enum data_type { TYPE_INT_SIGNED, TYPE_LONG_LONG_SIGNED, TYPE_CHAR_SIGNED };
      16      int shared_memory_fd = 0;
      17  
      18          shm_unlink(shared_memory_reference);
      19  
      20      if(-1 == (shared_memory_fd = shm_open(shared_memory_reference, O_CREAT|O_RDWR|O_EXCL, S_IRWXU|S_IRWXG|S_IRWXO)))
      21          {
      22                  fprintf(stdout, "%s%s\n", "Could not create a shared memory segment for ", shared_memory_reference);
      23                  return -1;
      24          }
      25  
      26      if(0 != ftruncate(shared_memory_fd, sizeof(int)))
      27      {
      28          printf("%s\n", "ftruncate has an error");
      29          perror("ftruncate error is ");
      30      }
      31      if(MAP_FAILED == (variable = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED, shared_memory_fd, 0)))
      32      {
      33          printf("%s\n", "POINTER ALLOCATION FAILED FOR MMAP");
      34      }
      35      perror("PError is: ");
      36      printf("%s%d\n", "IN SHARED MEMORY FNT1 ", *(int *) variable);
      37      *(int *) variable = DEBUG_ON;
      38      printf("%s%d\n", "IN SHARED MEMORY FNT2 ", *(int *) variable);
      39  
      40      close(shared_memory_fd);
      41      return 0;
      42  }
  </code></pre>
  
  <p>debug.c</p>
  
  <pre><code>     1  #include &lt;stdio.h&gt;
       2  
       3  #define DEBUG_ON 1
       4  #define DEBUG_OFF 0
       5  
       6  int print_debug(int *debug_flag, const char *title_string, const char *first_string, const char *second_string)
       7  {
       8      if(DEBUG_ON == *debug_flag)
       9      {
      10          fprintf(stdout, "%s%s%s%s%s\n", title_string, ": ", first_string," ",  second_string);
      11  
      12      }
      13      return 0;
      14  }
  </code></pre>
  
  <p>For completeness, the header files are shown below:</p>
  
  <p>debug.h</p>
  
  <pre><code>     1  extern int *debug_flag;
       2  
       3  int print_debug(int *debug_flag, const char *title_string, const char *first_string, const char *second_string);
       4  
       5  #define BLANK_STRING ""
       6  #define DEBUG_ON 1
       7  #define DEBUG_OFF 0
       8  
  </code></pre>
  
  <p>sharedmemory.h</p>
  
  <pre><code>    1   #define DEBUG_FLAG_1 "/debug_flag_1"
  </code></pre>
  
  <p>sharedmemoryv.h</p>
  
  <pre><code>    1   int shared_memory_variable(int type, void *variable, char *shared_memory_reference);
  </code></pre>