title: c program crashes when retrieving sizeof(buff)
body: <p>I am creating a program in C that splits a large text file into 10 segments, and then creates 10 threads with each thread generating a word count for each segment. I took the function <code>word_count</code> from this code: <a href="https://github.com/prateek-khatri/seaOfC/blob/master/frequencyMultiThread.c" rel="nofollow noreferrer">https://github.com/prateek-khatri/seaOfC/blob/master/frequencyMultiThread.c</a>. That program works fine for me, but when I tried to use <code>word_count</code> in my own program, it crashes when trying to get the size of the buffer. </p>
  
  <p>It seems like everything is ok in the function <code>getCurrentSegmentWordcount</code>, but when that function calls <code>word_count</code>, it crashes (segmentation fault) at the line <code>printf("sizeof Buff: %d", sizeof(buff));</code>.</p>
  
  <pre><code>#define _GNU_SOURCE
  #include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;unistd.h&gt;
  #define NUMBER_OF_THREADS 10
  
  //struct taken from reference:
  struct return_val{
      char wordlist[100][100]; //[chars][lines]
      int count[100];
  } *arr; //array of words
  
  void *print_hello_world(void * tid)
  {
      //This function prints the thread’s identifier and then exits.
      printf("Hello World. Greetings from thread %d\n", tid);
      pthread_exit(NULL);
  }
  
  void *word_count(void* num)
  { 
  
      int *ln = num;
      unsigned int line_number = *ln;
      //line_number++;
  
      printf("Thread %d\n",line_number);
  
      char cmd_p1[9] = "sed -n '\0";
      char cmd_p2[2];
      sprintf(cmd_p2,"%d",line_number); //stores string in buffer
      char cmd_p3[21] = "p' 'maintainers.txt'\0";
      char command[100];
      command[0] = '\0';
  
      //char * strcat ( char * destination, const char * source );
      //appends a copy of source to destination
      strcat(command,cmd_p1);
      strcat(command,cmd_p2);
      strcat(command,cmd_p3);
      usleep(line_number);
  
      char cmd[100] = " | tr [:space:] '\\n' | grep -v '^\\s*$' | sort | uniq -c | sort\0";
      strcat(command,cmd);
      printf("Command: %s\n",command);
      //fflush(stdout);
  
  
  
      FILE *in;
      in= popen(command, "r"); //read command and pipe into the shell
      rewind(in); //set file position to beginning of 'in'
      char buff[50];
      int counter = 0;
  
  
      //char * fgets ( char * str, int num, FILE * stream );
      //reads chars from stream and stores them as string into buff until all of buffer has been read
      printf("before\n");
      bool testBool = fgets(buff,sizeof(buff),in);
      printf("testBool: %d\n", testBool);
  
  
      //CRASH HAPPENS HERE:
      //buff 
      printf("sizeof Buff: %d", sizeof(buff));
  
  
      while(fgets(buff,sizeof(buff),in))
      {
          printf("fire 0.5");
          char c=' ';
          int i = 0;
          int cnt = atoi(buff); //converts string to int.. buff == # of chars in file?
          arr[line_number-1].count[counter] = cnt; //at this point line_number == 1
          printf("fire1\n");
  
          while(c!='\0')
          {
              c=buff[i];
              buff[i]=buff[i+6];
              i++;
          }
  
  
          int cnnt = 0;
          while(c!=' ')
          {
              c = buff[cnnt];
              cnnt++;
          }
          i=0;
          while(c!='\0')
          {
              c=buff[i];
              buff[i]=buff[i+cnnt];
              i++;
          }
          sprintf(arr[line_number-1].wordlist[counter],"%s",buff);
          printf("%d %s",arr[line_number-1].count[counter],arr[line_number-1].wordlist[counter]);
          counter++;
      }
      printf("final count: %d", counter);
      arr[line_number-1].count[counter] = -1;
  
  
      fclose(in);
  
  
  
      //pthread_exit(NULL); //didn't help to move here from getCurrentSegment...()
      return NULL;
  }
  
  
  
  void *getCurrentSegmentWordcount(void * tid) { //declaring file pointer (value?)
      int segment = tid;
      segment = segment + 1; //converts to int
      printf("segment/thread: %d \n", segment);
      char text[1000];
      //char buffer[150];
      FILE *fp = fopen("words.txt", "r");
      if(fp == NULL) {
          printf("null file");
      }
      int i = 0;
  
      long lSize;
      char *buffer;
      if( !fp ) perror("words.txt"),exit(1);
  
      fseek( fp , 0L , SEEK_END);
      lSize = ftell( fp );
      rewind( fp );
  
      buffer = calloc( 1, lSize+1 );
      if( !buffer ) fclose(fp),fputs("memory alloc fails",stderr),exit(1);
  
      if( 1!=fread( buffer , lSize, 1 , fp) )
        fclose(fp),free(buffer),fputs("entire read fails",stderr),exit(1);
  
      //printf(buffer);
  
      char *token = strtok(buffer, "~");
  
      if(segment == 1) {
          printf("segment 1: %s", token);
          word_count(&amp;segment);
      }
  
      if(segment == 2) {
          token = strtok(NULL,"~");
          printf("segment 2: %s", token);
      }
  
      if(segment == 3) {
          token = strtok(NULL,"~");
          token = strtok(NULL,"~");
          printf("segment 3: %s", token);
      }
  
      if(segment == 4) {
          token = strtok(NULL,"~");
          token = strtok(NULL,"~");
          token = strtok(NULL,"~");
          printf("segment 4: %s", token);
      }
  
      fclose(fp);
      free(buffer);
      //pthread_exit(NULL);//moving to end of word_count()
  }
  
  int main(int argc, char *argv[])
  {
      //The main program creates x threads and then exits.
      pthread_t threads[NUMBER_OF_THREADS];
      int status, i;
  
      for(i=0; i &lt; NUMBER_OF_THREADS; i++) {
          printf("Main here. Creating thread %d\n", i+1);
          status = pthread_create(&amp;threads[i], NULL, getCurrentSegmentWordcount, (void * )i);
          if (status != 0) {
              printf("Oops. pthread create returned error code %d\n", status);
              exit(-1);
          }
      }
      sleep(8);
      exit(NULL);
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Main here. Creating thread 1
  Main here. Creating thread 2
  segment/thread: 1 
  Main here. Creating thread 3
  segment 1: test(segment 1, handled my thread 1)
  Thread 1
  Main here. Creating thread 4
  Command: sed -n '1p' 'maintainers.txt' | tr [:space:] '\n' | grep -v '^\s*$' | sort | uniq -c | sort
  Main here. Creating thread 5
  segment/thread: 2 
  before
  segment/thread: 4 
  Main here. Creating thread 6
  segment 4: 
  test test test test (segment 4, handled by thread 4)
  Main here. Creating thread 7
  segment 2: 
  test test (segment 2, handled by thread 2)
  Main here. Creating thread 8
  Main here. Creating thread 9
  Main here. Creating thread 10
  segment/thread: 3 
  segment 3: 
  test test test (segment 3, handled by thread 3)
  segment/thread: 10 
  segment/thread: 9 
  segment/thread: 8 
  segment/thread: 5 
  segment/thread: 6 
  segment/thread: 7 
  testBool: 1
  Makefile:20: recipe for target 'all' failed
  make: *** [all] Segmentation fault (core dumped)
  </code></pre>