2016-12-07 17 views
2

私はRacerのmallocをしようとしていますが、Valgrindでエラーが発生し続けると、誰かが私がそれを持っている方法ではない理由を理解できますか?構造体と特定の変数を正しくmallocするには?

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のエラー:

[email protected]:~/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) 
[email protected]:~/Desktop/Threads$ 
+1

機能を離れる前に、それをfreeする必要があります... –

+0

@EugeneSh私はvalgrindエラーで投稿を更新します。 – ShaolinGOD

+0

'size_t sizeOfRacer = sizeof(Racer);'ここでの意図は? – wildplasser

答えて

3

あなたがライン49をマークし、なぜ私にはわかりません最初のバググライ​​ドエラーは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バイトを書いています。それらは2つの全く異なる値です:lengthOfNameは3ですが、lengthOfRearSide + 1は4です。

+0

したがって、lengthOfNameまたはlengthOfRearSideのどちらか一方のみを割り当てる必要がありますか? – ShaolinGOD

+0

@ShaolinGOD変数に格納する文字列に十分なメモリを割り当てる必要があります。 – melpomene

0

私はmiddleOfCarを調べました。それはどうしますか?

あなたがここでそれを割り当てる:

middleOfCar = (char *)malloc(remain); 

を、あなたはここでそれを使用します。

​​

が、何、その後どうなりますか?

何もないのでメモリがリークします。

たぶん、あなたはあなたが取得しているValgrindのエラーを*どの*を語っていないので、私はそれが `free`通話の欠如を好きではないと仮定します

+0

私はその機能の中で他のものを解放すべきですか? middleOfCarを解放すると、漏れが修正されました。 – ShaolinGOD

+0

@ ShaolinGOD - 参照を持たないものはすべて無料にする必要があります。 – 4386427

+0

valgrindはnRacer->グラフィックスを解放する必要があると言いますが、nRacerを返す必要があります。返す必要があるものをどのように解放すればよいですか? – ShaolinGOD

関連する問題