title: C free() invalid next size (normal)
body: <p>I'm fairly new to C and I can't seem to figure out what seems to be a pretty simple pointer problem.  My program adds line numbers to a file.  It reads in the file line by line and then adds a line number to the beginning of each line. It works fine on each file individually as you can see below:</p>
  
  <pre><code>soccer@soccer-Dell-DV051:~/code C$ ./a.out test.c
  soccer@soccer-Dell-DV051:~/code C$ ./a.out miscellaneousHeader.h
  soccer@soccer-Dell-DV051:~/code C$ ./a.out test.c miscellaneousHeader.h
  *** Error in `./a.out': free(): invalid next size (normal): 0x08648170 ***
  Segmentation fault (core dumped)
  soccer@soccer-Dell-DV051:~/code C$
  </code></pre>
  
  <p>but when I run them together I get the above error.  The following code is my program.</p>
  
  <p>Compiler.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "lineNumAdderHeader.h"
  #include "miscellaneousHeader.h"
  
  int main(int argc, char *argv[]){
      if (argc &lt; 2)
          fatal("in main(). Invalid number of arguments");
  
      int i = 1;
      while (i &lt; argc){
          lineNumAdder(argv[i]);
          i++;
      }
  }
  </code></pre>
  
  <p>I have narrowed the problem to the <code>lineNumPtr</code>. The error occurs when <code>lineNumPtr</code> is freed after the second file. If <code>lineNumPtr</code> is not freed, which I know is bad programming, the program works just fine.</p>
  
  <p>lineNumAdder.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include "miscellaneousHeader.h"
  #include "lineNumAdderHeader.h"
  
  int lineSizeChecker(FILE*, int);
  int lineNumChecker(char*);
  int fileSizeChecker(FILE*);
  
  
  void lineNumAdder(char* filename){
      int lineSpace, position, lineNumCheckerBoolean, numOfDigits, fileSpace;
      int lineNum = 1;
      char *lineNumPtr = NULL;
      char *numberedFile = NULL;
      char *nonNumberedLine = NULL;
      char *originalLine = NULL;
      FILE *file = errorCheckedFopen(filename, "r+w");
  
      while(1){
          position = ftell(file);
  
          if (position == 0){
              fileSpace = fileSizeChecker(file);
              numberedFile = errorCheckedMalloc(fileSpace);
          }
  
          lineSpace = lineSizeChecker(file, position);
          if (position == 0)
              originalLine = errorCheckedMalloc(lineSpace);
          else
              originalLine = realloc(originalLine, lineSpace);
  
          if (fgets(originalLine, lineSpace, file) == NULL)
              break;
  
          lineNumCheckerBoolean = lineNumChecker(originalLine);
  
          if (lineNumCheckerBoolean == 0){
              if (position == 0)
                  nonNumberedLine = errorCheckedMalloc(lineSpace - 9);
              else
                  nonNumberedLine = realloc(nonNumberedLine, lineSpace - 9);
  
              strcpy(nonNumberedLine, &amp;originalLine[9]);
          }
          else{
              if (position == 0)
                  nonNumberedLine = errorCheckedMalloc(lineSpace);
              else
                  nonNumberedLine = realloc(nonNumberedLine, lineSpace);
  
              strcpy(nonNumberedLine, originalLine);
  
              fileSpace += 8;
              numberedFile = realloc(numberedFile, fileSpace);
          }
  
          numOfDigits = intDigitFinder(lineNum);
          if (position == 0)
              lineNumPtr = errorCheckedMalloc(numOfDigits);
          else
              lineNumPtr = realloc(lineNumPtr, numOfDigits);
          sprintf(lineNumPtr, "%d", lineNum);
  
          strcat(numberedFile, "/*");
          strcat(numberedFile, lineNumPtr);
          strcat(numberedFile, "*/");
          if (lineNum &lt; 10)
              strcat(numberedFile, "    ");
          else if (lineNum &gt;= 10 &amp;&amp; lineNum &lt; 100)
              strcat(numberedFile, "   ");
          else if (lineNum &gt;= 100 &amp;&amp; lineNum &lt; 1000)
              strcat(numberedFile, "  ");
          else if (lineNum &gt;= 1000 &amp;&amp; lineNum &lt; 10000)
              strcat(numberedFile, " ");
          strcat(numberedFile, nonNumberedLine);
          lineNum++;
      }
  
      fclose(file);
      free(originalLine);
      free(nonNumberedLine);
      free(lineNumPtr);
      free(numberedFile);
  }
  
  int lineNumChecker(char *comment){
      if (sizeOf(comment) &lt; 8)
          return 1;
  
      if (comment[7] == '/' || comment[6] == '/' || comment[5] == '/' || comment[4] == '/')
          return 0;
      else
          return 1;
  }
  
  int lineSizeChecker(FILE *file, int position){
      int i = 2;
      int ch;
  
      while ((ch = fgetc(file)) != '\n' &amp;&amp; ch != EOF)
          i++;
  
      fseek(file, position, SEEK_SET);
  
      return i;
  }
  
  int fileSizeChecker(FILE *file){
      int i = 2;
  
      while (fgetc(file) != EOF)
          i++;
  
      fseek(file, 0, SEEK_SET);
  
      return i;
  }
  </code></pre>
  
  <p>miscellaneous.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include "miscellaneousHeader.h"
  
  void fatal(char*);
  
  int sizeOf(char *data){
      int i = 1;
  
      while(data[i-1] != '\n')
          i++;
  
      return i;
  }
  
  void *errorCheckedMalloc(size_t size){
      void *ptr = malloc(size);
  
      if (ptr == NULL)
          fatal("in errorCheckedMalloc(). Memory Allocation Failure\n");
      else
          return ptr;
  }
  
  FILE *errorCheckedFopen(char *filename, char *mode){
      FILE *file = fopen(filename, mode);
  
      if (file == NULL)
          fatal("in errorCheckedFopen(). File Opening Failure\n");
      else
          return file;
  }
  
  void fatal(char *errorMessage){
      char* completedErrorMessage = errorCheckedMalloc(sizeOf(errorMessage)+17);
  
      strcpy(completedErrorMessage, "[!!] Fatal Error ");
      strcat(completedErrorMessage, errorMessage);
  
      perror(completedErrorMessage);
  
      free(completedErrorMessage);
  
      exit(-1);
  }
  
  int intDigitFinder(int num){
      int digits = 0;
  
      do {
          num /= 10;
          digits++;
      } while (num != 0);
  
      return digits;
  }
  
  void *reMalloc(void *ptr, size_t size){
      char buf[strlen(ptr) + 1];
      strcpy(buf, ptr);
  
      free(ptr);
  
      ptr = errorCheckedMalloc(size);
  
      if(size &gt;= strlen(buf))
          strcpy(ptr, buf);
  
      return ptr;
  }
  </code></pre>
  
  <p>I apologize for the length. This is my first post and I wanted to make sure that I provided enough information for you guys to give me the best answers possible. Thank you for any and all answers.  They are much appriciated.</p>