Шустрый

Профиль
Группа: Участник
Сообщений: 50
Регистрация: 10.9.2007
Репутация: нет Всего: 1
|
Спасибо, полезная утилита. Но в данном случае мне она не разъяснила ситуацию, а только запутала: | Код | ==4956== Invalid read of size 4 ==4956== at 0x80485AB: AddNode (lab23_2.c:64) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== Address 0x418e024 is 4 bytes before a block of size 20 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804858E: AddNode (lab23_2.c:62) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x80485C3: AddNode (lab23_2.c:65) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== Address 0x418e020 is 8 bytes before a block of size 20 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804858E: AddNode (lab23_2.c:62) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x804861A: AddNode (lab23_2.c:70) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== Address 0x418e06c is 4 bytes before a block of size 12 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80485F2: AddNode (lab23_2.c:68) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048635: AddNode (lab23_2.c:71) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== Address 0x418e068 is 8 bytes before a block of size 12 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80485F2: AddNode (lab23_2.c:68) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x80486E9: AddNode (lab23_2.c:87) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== Address 0x418e0ac is 4 bytes before a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048701: AddNode (lab23_2.c:88) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== Address 0x418e0a8 is 8 bytes before a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x804873B: AddNode (lab23_2.c:91) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== Address 0x418e0e4 is 4 bytes before a block of size 20 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804871E: AddNode (lab23_2.c:89) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048753: AddNode (lab23_2.c:92) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== Address 0x418e0e0 is 8 bytes before a block of size 20 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804871E: AddNode (lab23_2.c:89) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x80487B5: AddNode (lab23_2.c:98) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== Address 0x418e12c is 4 bytes before a block of size 12 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804878D: AddNode (lab23_2.c:96) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x80487D0: AddNode (lab23_2.c:99) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== Address 0x418e128 is 8 bytes before a block of size 12 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804878D: AddNode (lab23_2.c:96) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid write of size 4 ==4956== at 0x804880B: AddNode (lab23_2.c:103) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== Address 0x418e0b4 is 0 bytes after a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid write of size 4 ==4956== at 0x8048818: AddNode (lab23_2.c:104) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== Address 0x418e0b8 is 4 bytes after a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048520: GetElementByIndex (lab23_2.c:45) ==4956== by 0x804869E: AddNode (lab23_2.c:80) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== Address 0x418e0b8 is 4 bytes after a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048520: GetElementByIndex (lab23_2.c:45) ==4956== by 0x8048AED: Delete (lab23_2.c:180) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e0b8 is 4 bytes after a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x804893C: DeleteTree (lab23_2.c:141) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e178 is 4 bytes after a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x80489E9: DeleteTree (lab23_2.c:153) ==4956== by 0x8048946: DeleteTree (lab23_2.c:141) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e1ec is 4 bytes before a block of size 12 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804878D: AddNode (lab23_2.c:96) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048A04: DeleteTree (lab23_2.c:154) ==4956== by 0x8048946: DeleteTree (lab23_2.c:141) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e1e8 is 8 bytes before a block of size 12 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804878D: AddNode (lab23_2.c:96) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048A40: DeleteTree (lab23_2.c:161) ==4956== by 0x8048946: DeleteTree (lab23_2.c:141) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e174 is 0 bytes after a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048A68: DeleteTree (lab23_2.c:163) ==4956== by 0x8048946: DeleteTree (lab23_2.c:141) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e174 is 0 bytes after a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid write of size 4 ==4956== at 0x8048A74: DeleteTree (lab23_2.c:164) ==4956== by 0x8048946: DeleteTree (lab23_2.c:141) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e178 is 4 bytes after a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048A94: DeleteTree (lab23_2.c:168) ==4956== by 0x8048946: DeleteTree (lab23_2.c:141) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e1a4 is 4 bytes before a block of size 20 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804871E: AddNode (lab23_2.c:89) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048AAC: DeleteTree (lab23_2.c:169) ==4956== by 0x8048946: DeleteTree (lab23_2.c:141) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e1a0 is 8 bytes before a block of size 20 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804871E: AddNode (lab23_2.c:89) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048950: DeleteTree (lab23_2.c:143) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e174 is 0 bytes after a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048984: DeleteTree (lab23_2.c:147) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e16c is 4 bytes before a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x804899C: DeleteTree (lab23_2.c:148) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e168 is 8 bytes before a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x80489E9: DeleteTree (lab23_2.c:153) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e12c is 4 bytes before a block of size 12 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804878D: AddNode (lab23_2.c:96) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048A04: DeleteTree (lab23_2.c:154) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e128 is 8 bytes before a block of size 12 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804878D: AddNode (lab23_2.c:96) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048A40: DeleteTree (lab23_2.c:161) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e0b4 is 0 bytes after a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048A68: DeleteTree (lab23_2.c:163) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e0b4 is 0 bytes after a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid write of size 4 ==4956== at 0x8048A74: DeleteTree (lab23_2.c:164) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e0b8 is 4 bytes after a block of size 4 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x80486CC: AddNode (lab23_2.c:85) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048A94: DeleteTree (lab23_2.c:168) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e0e4 is 4 bytes before a block of size 20 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804871E: AddNode (lab23_2.c:89) ==4956== by 0x8048CD7: main (lab23_2.c:257) ==4956== ==4956== Invalid read of size 4 ==4956== at 0x8048AAC: DeleteTree (lab23_2.c:169) ==4956== by 0x8048B01: Delete (lab23_2.c:183) ==4956== by 0x8048D03: main (lab23_2.c:259) ==4956== Address 0x418e0e0 is 8 bytes before a block of size 20 alloc'd ==4956== at 0x4024D0E: malloc (vg_replace_malloc.c:207) ==4956== by 0x804871E: AddNode (lab23_2.c:89) ==4956== by 0x8048CD7: main (lab23_2.c:257)
|
вот код с выводом на экран всех адресов при выделении\освобождении памяти: | Код | #include <stdio.h> #include <string.h> #include <malloc.h>
struct Node;
struct ChildNode{ struct ChildNode *prev; struct ChildNode *next; struct Node *element; }; typedef struct ChildNode childnode;
struct Node{ int index; double value; childnode *parent; childnode *children; }; typedef struct Node node;
struct List{ int count; struct List *prev; }; typedef struct List list;
node *first; int count = 0;
node *GetElementByIndex(int index, node *t) { node *temp = NULL; childnode *c;
if (t==NULL) return NULL; if (t->index==index) return t; c=t->children->next; if (c==NULL) return NULL; while (c!=NULL) { temp=GetElementByIndex(index,c->element); if (temp!=NULL) break; c=c->next; } if (temp!=NULL) return temp; return NULL; }
int AddNode(int index, double value) { node *t, *temp; childnode *c, *child;
if (first==NULL) { temp = (node *)malloc(sizeof(node)); printf ("temp malloc res=%p\n", temp); printf ("res[-1]=0x%08x\n", *(((int*)temp)-1)); printf ("res[-2]=0x%08x\n", *(((int*)temp)-2)); temp->value = value; temp->index = 1; temp->children = (childnode*)malloc(sizeof(childnode)); printf ("temp->children malloc res=%p\n", temp->children); printf ("res[-1]=0x%08x\n", *(((int*)temp->children)-1)); printf ("res[-2]=0x%08x\n", *(((int*)temp->children)-2)); temp->children->prev = NULL; temp->children->next = NULL; temp->children->element = NULL; first=temp; count=1; return 0; } t = GetElementByIndex(index,first); if (t==NULL) return -1;
++count; child = (childnode*)malloc(sizeof(child)); printf ("child malloc res=%p\n", child); printf ("res[-1]=0x%08x\n", *(((int*)child)-1)); printf ("res[-2]=0x%08x\n", *(((int*)child)-2)); temp = (node *)malloc(sizeof(node)); printf ("temp malloc res=%p\n", temp); printf ("res[-1]=0x%08x\n", *(((int*)temp)-1)); printf ("res[-2]=0x%08x\n", *(((int*)temp)-2)); temp->value = value; temp->index = count; temp->parent = child; temp->children = (childnode*)malloc(sizeof(childnode)); printf (" temp->children malloc res=%p\n", temp->children); printf ("res[-1]=0x%08x\n", *(((int*)temp->children)-1)); printf ("res[-2]=0x%08x\n", *(((int*)temp->children)-2)); temp->children->prev = NULL; temp->children->next = NULL; temp->children->element = NULL; child->next = NULL; child->element = temp; c=t->children; while (c->next!=NULL) c=c->next; child->prev = c; c->next = child; return 0; }
void PrintTree(const char *str, node *p) { childnode *c; char temp[50];
if (p==NULL) return; printf("%s%i: %0.2f\n",str,p->index,p->value); if (p->children->next==NULL) return; strcpy(temp,str); strcat(temp,"\t\0"); c=p->children->next; while (c!=NULL) { PrintTree(temp,c->element); c=c->next; } return; }
void DeleteTree(node *p) { childnode *c, *cTemp; printf("index: %i\n",p->index); c=p->children->next; while (c!=NULL) { DeleteTree(c->element); cTemp = c; c = c->next;
printf ("obrshenie %p\n", cTemp->prev); printf ("free %p\n", cTemp); printf ("free[-1]=0x%08x\n", *(((int*)cTemp)-1)); printf ("free[-2]=0x%08x\n", *(((int*)cTemp)-2)); free(cTemp); cTemp = NULL; } printf ("p->children free %p\n", p->children); printf ("free[-1]=0x%08x\n", *(((int*)p->children)-1)); printf ("free[-2]=0x%08x\n", *(((int*)p->children)-2));
free(p->children); if (first==p) first = NULL; else { if (p->parent->next!=NULL) p->parent->next->prev = p->parent->prev; p->parent->prev->next = p->parent->next; p->parent->element=NULL; } printf ("p free %p\n", p); printf ("free[-1]=0x%08x\n", *(((int*)p)-1)); printf ("free[-2]=0x%08x\n", *(((int*)p)-2));
free(p); }
void Delete(int index) { node *t;
if (first==NULL) return; t = GetElementByIndex(index,first); if (t==NULL) return; DeleteTree(t); return; }
void task(node *p, list **l, int *cnt) { int count = 0; list *t = (list*)malloc(sizeof(list)); childnode *c;
if (p==NULL) return;
c = p->children->next; while (c!=NULL) { ++count; task(c->element,l, cnt); c=c->next; } t->count = count; if (count>*cnt) *cnt = count; if ((*l)==NULL) { t->prev = NULL; (*l)=t; return; } t->prev = (*l); (*l)=t;
return; }
int Count(list *l, int cnt) { int c = 0; list *t;
if (l==NULL) return 0; while (l->prev != NULL) { if (l->count == cnt) ++c; t = l; l = l->prev; free(t); t = NULL; } if (l->count == cnt) ++c; free(l); return c; }
int main() { char c; int index, cnt; double value; list *l = NULL;
cnt = 0;
do { c = getchar(); switch(c) { case '1': PrintTree("", first); break; case '2': printf("Input index and value to input: "); scanf("%i%le",&index,&value); AddNode(index,value); break; case '3': printf("Input index to delete"); scanf("%i",&index); Delete(index); break; case '4': l = NULL; cnt=0; task(first,&l,&cnt); printf("Number: %i\n",Count(l, cnt)); break; } } while (c!='5'); }
|
ошибки чтения памяти получаются из-за printf'оф, а вот откуда ошибки записи не пойму. Это сообщение отредактировал(а) Pori - 25.5.2009, 00:18
|