Wesley
Wesley

Reputation: 89

How to malloc correctly for a struct and certain variables?

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?

Valgrind tells me the errors are at lines 49, 50, and 57... I marked them with arrows.

Here's my racer.c code:

#define _BSD_SOURCE
#include <stdlib.h> 
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <pthread.h>
#include <unistd.h>

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(&lock, NULL);
    srand(time(NULL));
}

// Creates a new racer.
Racer *makeRacer( char *name, int position ){
    int remain;
    int sizeOfRear = 0;
    char *frontSide = "o>";
    char *rearSide = "~0=";
    char *middleOfCar;

    int lengthOfName = strlen(name);
    int lengthOfRearSide = strlen(rearSide);
    size_t sizeOfRacer = sizeof(Racer);
->  Racer *nRacer = (Racer *)malloc(sizeOfRacer);
->  nRacer->graphic = (char *)malloc(lengthOfName);

    strncpy(nRacer->graphic, rearSide, lengthOfRearSide + 1);
    strcat(nRacer->graphic, name);
    sizeOfRear = strlen(nRacer->graphic);

    remain = MAX_CAR_LEN - sizeOfRear;
->  middleOfCar = (char *)malloc(remain);
    for (int x = 0; x < remain - 2; x++){
        middleOfCar[x] = '-';
    }
    strcat(middleOfCar, frontSide);
    strcat(nRacer->graphic, middleOfCar);
    nRacer->dist = 0;
    nRacer->row = position;
    return nRacer;
}

and here's the struct:

/// 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;

Valgrind Errors:

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> 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> Thread 2:
==18972== Invalid read of                                  ~X=two----o>
==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$ 

Upvotes: 2

Views: 173

Answers (2)

4386427
4386427

Reputation: 44274

I looked into middleOfCar. What happens to that?

You allocate it here:

middleOfCar = (char *)malloc(remain);

then you use it here:

middleOfCar[x] = '-';
    }
    strcat(middleOfCar, frontSide);
    strcat(nRacer->graphic, middleOfCar);

but what happens then?

Nothing more.... so you leak memory.

Maybe you need to free it before leaving the function

Upvotes: 0

melpomene
melpomene

Reputation: 85767

I don't know why you marked line 49, but the first valgrind error complains about line 52:

==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)

Which tells you that (by way of strncpy) you're writing to memory you don't own.

Line 52 is:

    strncpy(nRacer->graphic, rearSide, lengthOfRearSide + 1);

The memory for nRacer->graphic was allocated as:

    nRacer->graphic = (char *)malloc(lengthOfName);

So you allocated lengthOfName bytes, but you're writing lengthOfRearSide + 1 bytes. Those are two completely different values: lengthOfName is 3 but lengthOfRearSide + 1 is 4.

Upvotes: 3

Related Questions