Valgrind“无效写入4号”

时间:2017-08-02 18:55:42

标签: c valgrind

我一直在编写一个生成XML文件作为输出的程序,出于学习目的,我编写了一个包含我需要的所有函数的XML库。经过一些测试后,它似乎有时正常工作,有时则不正常,所以,根本不工作。我几乎没有valgrind的经验,所以通过“无效写入大小......”或“无效读取大小......”来理解它的含义有点难以理解。

Valgrind告诉我错误在这个函数内:

34: XML* CriarFilhoXML(XML* pai, char* n, char* v, char* a, BYTE t)
35: {
36:     if(pai == NULL)
37:    {
38:         printf("New XML tag parent unespecified.\n");
39:         return NULL;
40:     }
41: 
42:     XML* xml = NULL; // structure to be returned
43: 
44:     if(pai->prox == NULL) // no children
45:     {
46:         xml = (XML*) calloc(1, sizeof(XML));
47:         if(xml == NULL)
48:         {
49:             printf("No memory for new XML tag!\n");
50:             return NULL;
51:         }
52: 
53:         pai->prox = xml;
54:     }
55:     else // has children
56:     {
57:         pai->prox = (XML*) realloc(pai->prox, (pai->numfilhos + 1) * sizeof(XML));
58:         if(pai->prox == NULL) return NULL;
59: 
60:         xml = &pai->prox[pai->numfilhos]; // new children
61:     }
62: 
63:     pai->numfilhos += 1;
64: 
65:     xml->ant = pai; // previous level
66: 
67:     xml->nivel = pai->nivel + 1;
68:     xml->numfilhos = 0;
69: 
70:     xml->nome = n;
71:     xml->valor = v;
72: 
73:     xml->atributos = a;
74: 
75:     xml->tipo = t;
76: 
77:     xml->prox = NULL;
78: 
79:     return xml;
80: }

此函数创建一个新元素,将其添加到现有树中。它的参数是父级,名称,值,属性,类型,按顺序。

现在,valgrind在这个函数中给了我很多错误:

==9736== 1 errors in context 1 of 131:
==9736== Invalid write of size 4
==9736==    at 0x403D1B: CriarFilhoXML (XML.c:63)
==9736==    by 0x4033B9: AdicionarMotes (CSC.c:562)
==9736==    by 0x400D93: main (main.c:93)
==9736==  Address 0x550f10c is 284 bytes inside a block of size 336 free'd
==9736==    at 0x4C2CE8E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9736==    by 0x403CF5: CriarFilhoXML (XML.c:57)
==9736==    by 0x403152: AdicionarMotes (CSC.c:527)
==9736==    by 0x400D93: main (main.c:93)
==9736== 
==9736== 
==9736== 1 errors in context 2 of 131:
==9736== Invalid read of size 4
==9736==    at 0x403D16: CriarFilhoXML (XML.c:67)
==9736==    by 0x4033B9: AdicionarMotes (CSC.c:562)
==9736==    by 0x400D93: main (main.c:93)
==9736==  Address 0x550f108 is 280 bytes inside a block of size 336 free'd
==9736==    at 0x4C2CE8E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9736==    by 0x403CF5: CriarFilhoXML (XML.c:57)
==9736==    by 0x403152: AdicionarMotes (CSC.c:527)
==9736==    by 0x400D93: main (main.c:93)
==9736== 
==9736== 
==9736== 1 errors in context 3 of 131:
==9736== Invalid read of size 4
==9736==    at 0x403CFF: CriarFilhoXML (XML.c:60)
==9736==    by 0x4033B9: AdicionarMotes (CSC.c:562)
==9736==    by 0x400D93: main (main.c:93)
==9736==  Address 0x550f10c is 284 bytes inside a block of size 336 free'd
==9736==    at 0x4C2CE8E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9736==    by 0x403CF5: CriarFilhoXML (XML.c:57)
==9736==    by 0x403152: AdicionarMotes (CSC.c:527)
==9736==    by 0x400D93: main (main.c:93)
==9736== 
==9736== 
==9736== 1 errors in context 4 of 131:
==9736== Invalid write of size 8
==9736==    at 0x403CF9: CriarFilhoXML (XML.c:57)
==9736==    by 0x4033B9: AdicionarMotes (CSC.c:562)
==9736==    by 0x400D93: main (main.c:93)
==9736==  Address 0x550f138 is 328 bytes inside a block of size 336 free'd
==9736==    at 0x4C2CE8E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9736==    by 0x403CF5: CriarFilhoXML (XML.c:57)
==9736==    by 0x403152: AdicionarMotes (CSC.c:527)
==9736==    by 0x400D93: main (main.c:93)

这是什么错误?我该如何解决?

感谢您的关注。

0 个答案:

没有答案