title: stack corruption in coredump ? gcc/c
body: <p>I am noticing a strange problem when I attempt to use the signal handler to dump global data to a text file and generate the core file. I would expect the data dumped to the file to be the same as that is present in the core file (it is the same global data) </p>
  
  <p>in a header file foo.h</p>
  
  <pre><code>extern char buffer[100][80] ; // Hundred records each of length 80 characters
  </code></pre>
  
  <p>in foo.c</p>
  
  <pre><code>char buffer[100][80];
  
  .. in a loop ..
  snprintf(buffer[i],80,"%s:%d recorded idx = %d\n",__FUNCTION__,__LINE__,i);
  </code></pre>
  
  <p>in signal_handler.c</p>
  
  <pre><code>.. in a loop ..
  fprintf(..dump data to text file..)
  </code></pre>
  
  <p>The data is dumped to the text file alright. I run the program in gdb and I issue the ABRT signal (the signal I am handling) via kill. In gdb I see</p>
  
  <pre><code>gdb) p &amp;buffer[0]
  $3 = (char (*)[80]) 0x1002c8970 
  </code></pre>
  
  <p>I continue and generate the core file. In the core dump I see </p>
  
  <pre><code>(gdb) p &amp;buffer[0]
  $2 = (char (*)[80]) 0x1002c9a80 
  </code></pre>
  
  <p>the difference between the two positions is 1110. </p>
  
  <p>My question is why do I see this discrepancy in the core file ? Any leads would be appreciated!</p>
  
  <p>Thanks
  John </p>
  
  <p>EDIT To clarify, the problem is not in generating the core via GDB 
  Full code without signal handlers to isolate the problem. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define MAX 100
  char buffer[100][80];
  
  int main()
  {
  int i = 0;
  int idx = 0;
  FILE *fp = NULL;
  fp = fopen("test.txt","w");
  if (!fp)
      exit(1);
  
  for (i=0; i &lt; 5500; i++) {
      snprintf(buffer[idx],80,"%s:%d idx = %d\n",__FUNCTION__, __LINE__, i);
      idx = ((idx + 1)% MAX);
  }
  for (i = 0 ; i &lt; MAX; i++)
      fprintf(fp,"%s",buffer[i]);
  
  fclose(fp);
  abort();
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>The problem is not when I am trying to run in GDB, the problem is that in the core file generated, </p>
  
  <p>gdb) p buffer[0]</p>
  
  <p>$2 "c0 - idx = 54\n", '\0' , "main:20 0x7ef9524"</p>
  
  <p>the buffer is offset by 1110 bytes. I had used GDB to check if the buffer was corrupted. Sorry about the confusion.</p>