J'ai été la programmation pendant un moment mais je suis nouveau à C. J'ai cette implémentation de liste liée dans ansi C que j'ai besoin de tester. J'ai réduit le problème à un problème avec une écriture invalide. J'ai couru le code par Valgrind et reçu la sortie suivante:Écriture invalide avec strcpy
==18131== Invalid write of size 1
==18131== at 0x4C2C0CC: __GI_strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64 linux.so)
==18131== by 0x40089B: main (in /home/btm7984/hw3/TestList)
==18131== Address 0x51f1388 is 0 bytes after a block of size 8 alloc'd
==18131== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18131== by 0x400880: main (in /home/btm7984/hw3/TestList)
==18131==
==18131== Invalid write of size 1
==18131== at 0x4C2C0DF: __GI_strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18131== by 0x40089B: main (in /home/btm7984/hw3/TestList)
==18131== Address 0x51f138e is 6 bytes after a block of size 8 alloc'd
==18131== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18131== by 0x400880: main (in /home/btm7984/hw3/TestList)
==18131==
--18131-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting
--18131-- si_code=1; Faulting address: 0x6D4FCAA; sp: 0x402bdae00
Tout ce que je peux savoir de c'est que j'allouons quelque chose de mal. Je pense que ça doit être avec ma ligne strcpy. Je ne sais vraiment pas comment aborder cette question. Ce qui suit est mon utilisation de l'interface LinkedLists. InitLinkedLists, AddToBackOfList et DestroyList sont tous définis dans cette interface. En conclusion, je pense que strcpy provoque une écriture invalide et je ne sais pas pourquoi.
Toute aide serait grandement appréciée. Merci d'avance.
EDIT: ElementStructs est défini comme suit:
typedef struct ElementStructs
{
/* Application Specific Definitions */
int index;
char str[100];
} ElementStructs;
Comment est 'ElementStructs' défini? – cnicutar
'p = malloc (sizeof p)' est toujours faux. – melpomene
typedef struct ElementStructs { /* Définitions spécifiques à l'application */ int index; char str [100]; } ElementStructs; – user1935333