如何正确地为结构和某些变量malloc?

时间:2016-12-07 22:01:48

标签: c malloc valgrind

我试图为Racer尝试malloc,但我一直在Valgrind遇到错误,有人可以帮助我理解为什么它不是我的方式吗?

Valgrind告诉我错误在第49,50和57行......我用箭头标记了它们。

这是我的racer.c代码:

#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;
}

以及结构:

/// 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错误:

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$ 

2 个答案:

答案 0 :(得分:3)

我不知道你为什么标记第49行,但是第一个valgrind错误抱怨第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)

它告诉你(通过strncpy)你写的是你不拥有的记忆。

第52行是:

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

nRacer->graphic的内存分配为:

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

所以你分配了lengthOfName个字节,但是你正在编写lengthOfRearSide + 1个字节。这是两个完全不同的值:lengthOfName为3,但lengthOfRearSide + 1为4。

答案 1 :(得分:0)

我调查了middleOfCar。那会怎么样?

你在这里分配:

middleOfCar = (char *)malloc(remain);

然后你在这里使用它:

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

但接下来会发生什么?

没有更多......所以你泄漏了记忆。

在离开函数

之前,可能需要free