私は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$
機能を離れる前に、それを
free
する必要があります... –@EugeneSh私はvalgrindエラーで投稿を更新します。 – ShaolinGOD
'size_t sizeOfRacer = sizeof(Racer);'ここでの意図は? – wildplasser