valgrind, finde den Fehler nicht

OP #7600504
Lesenswert?

Hallo. Ein kleines Problem bei einem Programm. Habe valgrind mal laufen lassen und folgenden Fehler, bei dem ich nicht ganz verstehe, was er meint. “Invalid read/write of size 8”. Vermutlich eine double Variable? Was genau habe ich falsch gemacht? Das Programm selbst läuft wie es sollte.

1
valgrind ./a.out --leak-check=yes
2
==29373== Memcheck, a memory error detector
3
==29373== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
4
==29373== Using Valgrind-3.20.0 and LibVEX; rerun with -h for copyright info
5
==29373== Command: ./a.out --leak-check=yes
6
==29373== 
7

8
Array 1
9
==29373== Invalid read of size 8
10
==29373==    at 0x4007F0: shift_array (in /home/michael/Documents/test/a.out)
11
==29373==    by 0x400A5B: main (in /home/michael/Documents/test/a.out)
12
==29373==  Address 0x4a6e090 is 0 bytes after a block of size 0 alloc'd
13
==29373==    at 0x4838744: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
14
==29373==    by 0x400A01: main (in /home/michael/Documents/test/a.out)
15
==29373== 
16
==29373== Invalid write of size 8
17
==29373==    at 0x40086A: shift_array (in /home/michael/Documents/test/a.out)
18
==29373==    by 0x400A5B: main (in /home/michael/Documents/test/a.out)
19
==29373==  Address 0x4a6e088 is 8 bytes before a block of size 0 alloc'd
20
==29373==    at 0x4838744: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
21
==29373==    by 0x400A01: main (in /home/michael/Documents/test/a.out)
22
==29373== 
23

24
Shift Array 1 left by 1
25

26
Array 2 average
27
==29373== 
28
==29373== HEAP SUMMARY:
29
==29373==     in use at exit: 0 bytes in 0 blocks
30
==29373==   total heap usage: 5 allocs, 5 frees, 1,056 bytes allocated
31
==29373== 
32
==29373== All heap blocks were freed -- no leaks are possible
33
==29373== 
34
==29373== For lists of detected and suppressed errors, rerun with: -s
35
==29373== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Angehängte Dateien:
#7600530
Lesenswert?

Hier das Beispiel unter Ubuntu:

1
gcc main.c -g -o main
2
valgrind ./main 0

Ergibt (Auszug):

1
==31658== Invalid read of size 8
2
==31658==    at 0x109317: shift_array (main.c:30)
3
==31658==    by 0x1095A9: main (main.c:85)

Die Codestelle ist:

1
double array_first_pos = a->data[0];

(Diese Codestelle hätte ich jetzt auch erwartet, dies ist offensichtlich: Bei 0 Einträgen ist dies 'out of range'.

OP #7600544
Lesenswert?

Niklas G. schrieb:

Kompilier mit Debug-Informationen, dann sind die Ausgaben von valgrind hilfreicher.

Das mache ich wie? Bin unter Linux mit gcc noch nicht so ganz fit. Kompiliert hat es ohne Fehler.

Andreas B. schrieb:

shift_array crasht wenn das Array die grösse 0 hat.

Welche Parameter hast du verwendet?

Andreas B. schrieb:

Die Codestelle ist:double array_first_pos = a->data[0]; (Diese Codestelle hätte ich jetzt auch erwartet, dies ist offensichtlich: Bei 0 Einträgen ist dies 'out of range'.

Du hast recht. Ohne Argument hat es 0.

Alexander S. schrieb:

Du hast beim Aufruf von valgrind das Argument für a.out vergessen.

Alexander S. schrieb:

schreibe es sovalgrind --leak-check=yes ./a.out 64

So hatte ich es anfangs, hatte aber nicht funktioniert. Vermutlich vertippt und danach den Parameter vergessen. Danke nochmal an Alle.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren