2016-04-30 1 views
-1

Je possède ce morceau de code:pointeurs vide, affectation de mémoire et valgrind

void *data = calloc(1, sizeof(char)+sizeof(float)+sizeof(char)); // line 56 

*((char *) data) = 'a'; // line 58 
*((float *) data + sizeof(char)) = 0.2f; // line 59 
*((char *) data + sizeof(float) + sizeof(char)) = 'a'; // line 60 

printf("%c ", *((char *) data)); // line 62 
printf("%f ", *((float *) data + sizeof(char))); // line 63 
printf("%c ", *((char *) data + sizeof(float) + sizeof(char))); // line 64 
printf("\n"); 

Et basiquement ce que je suis en train de faire est d'économiser dans la même région de mémoire différents types de variables et leurs valeurs. Pour ce faire, j'utilise des pointeurs vides et des offsets de variables, et tout fonctionne bien, MAIS, valgrind continue à me dire que j'ai des lectures et des écritures invalides, et je ne comprends pas pourquoi. Tout a un sens à mon avis, mais Valgrind n'est pas d'accord.

Voici la sortie de valgrind:

==5829== Memcheck, a memory error detector 
==5829== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. 
==5829== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info 
==5829== Command: ./a.out 
==5829== 
==5829== Invalid write of size 4 
==5829== at 0x400909: main (Main.c:59) 
==5829== Address 0x51f6044 is 4 bytes inside a block of size 6 alloc'd 
==5829== at 0x4C2AA98: calloc (vg_replace_malloc.c:711) 
==5829== by 0x4008ED: main (Main.c:56) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E83CEE: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E83ED6: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E7D9C0: __mpn_extract_double (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E843C9: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E83FFA: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E845E2: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E85504: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E7C2E8: __mpn_lshift (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E85508: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E7C2EB: __mpn_lshift (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E85508: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E85546: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E85568: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E856CC: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E85A54: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E7C904: __mpn_mul_1 (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E85A6F: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E7C98F: __mpn_mul_1 (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E85A6F: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E7CA02: __mpn_mul_1 (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E85A6F: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E85A83: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E85A92: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E7C3F8: __mpn_rshift (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E85DF1: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E7C41D: __mpn_rshift (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E85DF1: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E849F1: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E7C904: __mpn_mul_1 (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E849F8: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E7C928: __mpn_mul_1 (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E849F8: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E84A40: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E836D9: hack_digit (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E84A58: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E7C904: __mpn_mul_1 (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E836E0: hack_digit (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E84A58: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Use of uninitialised value of size 8 
==5829== at 0x4E7C928: __mpn_mul_1 (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E836E0: hack_digit (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E84A58: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E84A63: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E84A71: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E85093: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E85099: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E853BD: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E853E2: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E8541D: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E84BDF: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E84E36: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E84FF9: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E84FDB: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4EAFC4D: [email protected]@GLIBC_2.2.5 (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E85C90: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4EAFC7A: [email protected]@GLIBC_2.2.5 (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E85C90: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Conditional jump or move depends on uninitialised value(s) 
==5829== at 0x4E85C94: __printf_fp (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E82D9D: vfprintf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4E88D28: printf (in /usr/lib64/libc-2.22.so) 
==5829== by 0x400951: main (Main.c:63) 
==5829== 
==5829== Syscall param write(buf) points to uninitialised byte(s) 
==5829== at 0x4F27C10: __write_nocancel (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4EAE14E: [email protected]@GLIBC_2.2.5 (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4EAF8F8: [email protected]@GLIBC_2.2.5 (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4EAFD0A: [email protected]@GLIBC_2.2.5 (in /usr/lib64/libc-2.22.so) 
==5829== by 0x4EA6A47: putchar (in /usr/lib64/libc-2.22.so) 
==5829== by 0x40097A: main (Main.c:65) 
==5829== Address 0x4022004 is in a rw- anonymous segment 
==5829== 
a 0.199592 a 
==5829== 
==5829== HEAP SUMMARY: 
==5829==  in use at exit: 6 bytes in 1 blocks 
==5829== total heap usage: 1 allocs, 0 frees, 6 bytes allocated 
==5829== 
==5829== LEAK SUMMARY: 
==5829== definitely lost: 6 bytes in 1 blocks 
==5829== indirectly lost: 0 bytes in 0 blocks 
==5829==  possibly lost: 0 bytes in 0 blocks 
==5829== still reachable: 0 bytes in 0 blocks 
==5829==   suppressed: 0 bytes in 0 blocks 
==5829== Rerun with --leak-check=full to see details of leaked memory 
==5829== 
==5829== For counts of detected and suppressed errors, rerun with: -v 
==5829== Use --track-origins=yes to see where uninitialised values come from 
==5829== ERROR SUMMARY: 103 errors from 42 contexts (suppressed: 0 from 0) 

Si quelqu'un pouvait me aider pourquoi est-valgrind tellement en colère que ce serait formidable.

Merci beaucoup!

+4

Si vous essayez d'obtenir la localité de données, vous pouvez simplement créer une structure. Si vous essayez d'utiliser une variable pour plusieurs types, vous devez probablement utiliser une union. Si vous êtes juste en train d'expérimenter, cool, mais c'est une idée horrible pour le code de production. – bodangly

+0

Il a des problèmes d'alignement en fonction du type de CPU. Il ne peut donc pas être utilisé simplement pour déréférencer directement les données supérieures à char à partir de l'adresse du point de caractère suivant. – BLUEPIXY

Répondre

2

Valgrind est fâché parce que vous utilisez les mauvaises adresses.

Vos déclarations sont tous à l'aide pointeur Arithmétique, c'est pas Arithmétique entier.

Par exemple:

*((float *) data + sizeof(char)) 

permet d'ajouter une float taille décalage aux données, et non "1".

Vous avez besoin des types de pointeurs corrects pour votre arithmétique et, une fois que vous avez calculé le décalage correct, transtypez le type que vous souhaitez utiliser.

+0

Je suis tellement bête. Merci beaucoup bro! – PatriqDesigns