title: How to malloc correctly for a struct and certain variables?
body: <p>I'm trying to malloc for Racer but I keep getting errors in Valgrind can someone help me understand why it is not the way I have it?</p>
  
  <p>Valgrind tells me the errors are at lines 49, 50, and 57... I marked them with arrows.</p>
  
  <p>Here's my racer.c code:</p>
  
  <pre><code>#define _BSD_SOURCE
  #include &lt;stdlib.h&gt; 
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;time.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;unistd.h&gt;
  
  long waitTime = DEFAULT_WAIT;
  pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  
  // Does the setup work for all racers at the start of the program.
  void initRacers( long milliseconds ){
      clear();
      if (milliseconds != 0){
          waitTime = milliseconds;
      }
      pthread_mutex_init(&amp;lock, NULL);
      srand(time(NULL));
  }
  
  // Creates a new racer.
  Racer *makeRacer( char *name, int position ){
      int remain;
      int sizeOfRear = 0;
      char *frontSide = "o&gt;";
      char *rearSide = "~0=";
      char *middleOfCar;
  
      int lengthOfName = strlen(name);
      int lengthOfRearSide = strlen(rearSide);
      size_t sizeOfRacer = sizeof(Racer);
  -&gt;  Racer *nRacer = (Racer *)malloc(sizeOfRacer);
  -&gt;  nRacer-&gt;graphic = (char *)malloc(lengthOfName);
  
      strncpy(nRacer-&gt;graphic, rearSide, lengthOfRearSide + 1);
      strcat(nRacer-&gt;graphic, name);
      sizeOfRear = strlen(nRacer-&gt;graphic);
  
      remain = MAX_CAR_LEN - sizeOfRear;
  -&gt;  middleOfCar = (char *)malloc(remain);
      for (int x = 0; x &lt; remain - 2; x++){
          middleOfCar[x] = '-';
      }
      strcat(middleOfCar, frontSide);
      strcat(nRacer-&gt;graphic, middleOfCar);
      nRacer-&gt;dist = 0;
      nRacer-&gt;row = position;
      return nRacer;
  }
  </code></pre>
  
  <p>and here's the struct:</p>
  
  <pre><code>/// Rcr struct represents a racer position and display graphic.
  ///
  typedef struct Rcr {
  
      /// current distance from starting line of the rear of the car
      ///
      int dist;
  
      /// vertical position of the racer, i.e. "racing lane"
      ///
      int row;
  
      /// graphic: the drawable text
      ///
      char *graphic;
  
  } Racer;
  </code></pre>
  
  <p>Valgrind Errors:</p>
  
  <pre><code>ShaolinGOD@comp:~/Desktop/Threads$ valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./pt-cruisers one two
  ==18972== Memcheck, a memory error detector
  ==18972== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
  ==18972== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
  ==18972== Command: ./pt-cruisers one two
  ==18972== 
  ==18972== Invalid write of size 1
  ==18972==    at 0x4C31644: __strncpy_sse2_unaligned (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x401208: makeRacer (racer.c:52)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972==  Address 0x5420093 is 0 bytes after a block of size 3 alloc'd
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4011DD: makeRacer (racer.c:50)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  ==18972== Invalid read of size 1
  ==18972==    at 0x4C30C14: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x40121F: makeRacer (racer.c:53)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972==  Address 0x5420093 is 0 bytes after a block of size 3 alloc'd
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4011DD: makeRacer (racer.c:50)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  ==18972== Invalid write of size 1
  ==18972==    at 0x4C30C30: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x40121F: makeRacer (racer.c:53)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972==  Address 0x5420093 is 0 bytes after a block of size 3 alloc'd
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4011DD: makeRacer (racer.c:50)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  ==18972== Invalid write of size 1
  ==18972==    at 0x4C30C3F: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x40121F: makeRacer (racer.c:53)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972==  Address 0x5420096 is 3 bytes after a block of size 3 alloc'd
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4011DD: makeRacer (racer.c:50)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  ==18972== Invalid read of size 1
  ==18972==    at 0x4C30F74: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x40122F: makeRacer (racer.c:54)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972==  Address 0x5420093 is 0 bytes after a block of size 3 alloc'd
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4011DD: makeRacer (racer.c:50)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  ==18972== Conditional jump or move depends on uninitialised value(s)
  ==18972==    at 0x4C30C17: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x401289: makeRacer (racer.c:61)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972==  Uninitialised value was created by a heap allocation
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x40124A: makeRacer (racer.c:57)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  ==18972== Invalid write of size 1
  ==18972==    at 0x4C30C3F: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x401289: makeRacer (racer.c:61)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972==  Address 0x54200e6 is 0 bytes after a block of size 6 alloc'd
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x40124A: makeRacer (racer.c:57)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  ==18972== Invalid read of size 1
  ==18972==    at 0x4C30C14: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4012A0: makeRacer (racer.c:62)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972==  Address 0x5420093 is 0 bytes after a block of size 3 alloc'd
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4011DD: makeRacer (racer.c:50)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  ==18972== Invalid write of size 1
  ==18972==    at 0x4C30C30: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4012A0: makeRacer (racer.c:62)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972==  Address 0x5420096 is 3 bytes after a block of size 3 alloc'd
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4011DD: makeRacer (racer.c:50)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  ==18972== Invalid read of size 1
  ==18972==    at 0x4C30C33: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4012A0: makeRacer (racer.c:62)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972==  Address 0x54200e6 is 0 bytes after a block of size 6 alloc'd
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x40124A: makeRacer (racer.c:57)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  ==18972== Invalid write of size 1
  ==18972==    at 0x4C30C3F: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4012A0: makeRacer (racer.c:62)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972==  Address 0x542009c is 9 bytes after a block of size 3 alloc'd
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4011DD: makeRacer (racer.c:50)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  0=two----o&gt; Thread 3:
  ==18972== Invalid read of size 1
  ==18972==    at 0x401413: run (racer.c:116)
  ==18972==    by 0x4E416F9: start_thread (pthread_create.c:333)
  ==18972==  Address 0x5420183 is 0 bytes after a block of size 3 alloc'd
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4011DD: makeRacer (racer.c:50)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
                      ~X=one----o&gt; Thread 2:
  ==18972== Invalid read of                                  ~X=two----o&gt;
  ==18972==    at 0x4013C7: run (racer.c:107)
  ==18972== HEAP SUMMARY:F9: start_thread (pthread_create.c:333)
  ==18972==     in use at exit: 50 bytes in 6 blocksock of size 3 alloc'd
  ==18972==   total heap usage: 14 allocs, 8 frees, 3,288 bytes allocatedeck-amd64-linux.so)
  ==18972==    by 0x4011DD: makeRacer (racer.c:50)
  ==18972== Thread 1:00F4F: main (pt-cruisers.c:74)
  ==18972== 6 bytes in 2 blocks are indirectly lost in loss record 1 of 3
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4011DD: makeRacer (racer.c:50)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  ==18972== 12 bytes in 2 blocks are definitely lost in loss record 2 of 3
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x40124A: makeRacer (racer.c:57)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  ==18972== 38 (32 direct, 6 indirect) bytes in 2 blocks are definitely lost in loss record 3 of 3
  ==18972==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==18972==    by 0x4011CC: makeRacer (racer.c:49)
  ==18972==    by 0x400F4F: main (pt-cruisers.c:74)
  ==18972== 
  ==18972== LEAK SUMMARY:
  ==18972==    definitely lost: 44 bytes in 4 blocks
  ==18972==    indirectly lost: 6 bytes in 2 blocks
  ==18972==      possibly lost: 0 bytes in 0 blocks
  ==18972==    still reachable: 0 bytes in 0 blocks
  ==18972==         suppressed: 0 bytes in 0 blocks
  ==18972== 
  ==18972== For counts of detected and suppressed errors, rerun with: -v
  ==18972== ERROR SUMMARY: 797 errors from 15 contexts (suppressed: 0 from 0)
  ShaolinGOD@comp:~/Desktop/Threads$ 
  </code></pre>