main:malloc.c:2372:sysmalloc:断言...失败

时间:2015-12-08 18:15:52

标签: c malloc

我一直致力于一个项目,其中我需要一个包含患者信息的列表。此列表必须保证优先级和有限的等待。

长话短说,我收到了这个错误:

main:malloc.c:2372:sysmalloc:断言`(old_top ==(((mbinptr)(((char *)&amp;((av) - &gt; bins [((1) - 1)* 2 ])) - __builtin_offsetof(struct malloc_chunk,fd))))&amp;&amp; old_size == 0)|| ((unsigned long)(old_size)&gt; =(unsigned long)(((__ builtin_offsetof(struct malloc_chunk,fd_nextsize))+((2 *(sizeof(size_t))) - 1))&amp;〜((2 * (sizeof(size_t))) - 1)))&amp;&amp;((old_top) - &gt; size&amp; 0x1)&amp;&amp;((unsigned long)old_end&amp; pagemask)== 0)'失败。< / p>

  

list.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "structFifo.h"
//------------------------------------------
typedef struct _nodo{
    schedato Pschedato;
    int age;
    struct _nodo *next;
    struct _nodo *prev;
}nodo;
typedef nodo* lista; 
//------------------------------------------

lista riavvolgi(lista l);
//------------------------------------------
lista rimuovi_in_testa(lista l){
    while(l->next!=NULL){
        l=l->next;
    }   
    l=l->prev;
    free(l->next);
    l->next=NULL;
    l=riavvolgi(l);
return l;}


//------------------------------------------
lista crea_nodo(schedato *tipo){
    lista nodo=(lista)malloc(sizeof(nodo));
    nodo->Pschedato=*tipo;
    nodo->next=NULL;
    nodo->prev=NULL;
    nodo->age=0;
    printf("--[CREANODO]--// n.reparto %d n_sintomo %d gravita %d codicecartella %d\n",nodo->Pschedato.n_reparto,nodo->Pschedato.n_sintomo,nodo->Pschedato.gravita,nodo->Pschedato.codiceCartella);
    printf("grandezza nodo allocato %d\n",(int)sizeof(nodo));
return nodo;}

//-------------------------------------------
lista inserisci(lista l, schedato * tipo){
lista nodo=crea_nodo(tipo);
printf("Nuovo nodo creato e inizializzato, procedo con l'inserimento\n");
if(l==NULL){
    printf("lista vuota,metto in testa\n");
    //printf("N.Reparto:%d ; N.Sintomo:%d ; Gravita:%d ; CodiceCartella:%d\n",tipo->n_reparto,tipo->n_sintomo,tipo->gravita,tipo->codiceCartella);  
    return nodo;
}
else{
    printf("ELSE\n");
    while((l->next!=NULL) && (l->next->Pschedato.gravita<nodo->Pschedato.gravita)){
        if(l->next->age<2){ 
        l->next->age++;
        l=l->next;  
        }
        else{
        break;      
        }

    }
    nodo->next=l->next;
    nodo->prev=l->prev;
    l->next=nodo;
    nodo->next->prev=nodo;
    nodo=riavvolgi(nodo);
}
return nodo;}
  

的main.c

#include "lista.c"
main(){
    lista listaRep=NULL;
    printf("Grandezza lista allocata %d\n",(int)sizeof(listaRep));
    schedato p1;
    schedato p2;
    schedato p3;
    schedato p4;
    schedato p5;
//------------------------------
    p1.n_reparto=1;
    p1.n_sintomo=3;
    p1.gravita=3;
    p1.codiceCartella=1000;
//----------------------------
    p2.n_reparto=1;
    p2.n_sintomo=4;
    p2.gravita=4;
    p2.codiceCartella=1001;
//----------------------------
    p3.n_reparto=1;
    p3.n_sintomo=5;
    p3.gravita=5;
    p3.codiceCartella=1002;
//----------------------------
    p4.n_reparto=1;
    p4.n_sintomo=6;
    p4.gravita=6;
    p4.codiceCartella=1003;
//----------------------------
    p5.n_reparto=1;
    p5.n_sintomo=7;
    p5.gravita=7;
    p5.codiceCartella=1004;

    listaRep=inserisci(listaRep,&p1);
    listaRep=inserisci(listaRep,&p2);
    listaRep=inserisci(listaRep,&p3);
    listaRep=inserisci(listaRep,&p4);
    listaRep=inserisci(listaRep,&p5);
    printf("La lista sarà stampata partendo dall'ultimo elemento(che verrà visto come il primo, leggere al contrario)\n");
    stampaLista(listaRep);


}
  

structFifo.h

typedef struct{
int n_reparto;
int n_sintomo;
int gravita;
int codiceCartella;
}schedato;

我也有valgrind的输出:

    --6112-- REDIR: 0x4eb9750 (malloc) redirected to 0x4c2ab10 (malloc)
==6112== Invalid write of size 8
==6112==    at 0x40069E: crea_nodo (lista.c:32)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112==  Address 0x51fc048 is 0 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== Invalid write of size 8
==6112==    at 0x4006A6: crea_nodo (lista.c:33)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112==  Address 0x51fc058 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== Invalid write of size 8
==6112==    at 0x4006B2: crea_nodo (lista.c:34)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112==  Address 0x51fc060 is not stack'd, malloc'd or (recently) free'd
==6112== 
==6112== Invalid write of size 4
==6112==    at 0x4006BE: crea_nodo (lista.c:35)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112==  Address 0x51fc050 is 8 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== Invalid read of size 4
==6112==    at 0x4006C9: crea_nodo (lista.c:36)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112==  Address 0x51fc04c is 4 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== Invalid read of size 4
==6112==    at 0x4006D0: crea_nodo (lista.c:36)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112==  Address 0x51fc048 is 0 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
--[CREANODO]--// n.reparto 1 n_sintomo 3 gravita 3 codicecartella 1000
grandezza nodo allocato 8
--6112-- REDIR: 0x4ebfac0 (strlen) redirected to 0x4c2e0e0 (strlen)
Nuovo nodo creato e inizializzato, procedo con l'inserimento
lista vuota,metto in testa
==6112== Invalid write of size 8
==6112==    at 0x40069E: crea_nodo (lista.c:32)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc098 is 0 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
==6112== Invalid write of size 8
==6112==    at 0x4006A6: crea_nodo (lista.c:33)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc0a8 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
==6112== Invalid write of size 8
==6112==    at 0x4006B2: crea_nodo (lista.c:34)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc0b0 is not stack'd, malloc'd or (recently) free'd
==6112== 
==6112== Invalid write of size 4
==6112==    at 0x4006BE: crea_nodo (lista.c:35)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc0a0 is 8 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
==6112== Invalid read of size 4
==6112==    at 0x4006C9: crea_nodo (lista.c:36)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc09c is 4 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
==6112== Invalid read of size 4
==6112==    at 0x4006D0: crea_nodo (lista.c:36)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc098 is 0 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
--[CREANODO]--// n.reparto 1 n_sintomo 4 gravita 4 codicecartella 1001
grandezza nodo allocato 8
Nuovo nodo creato e inizializzato, procedo con l'inserimento
ELSE
==6112== Invalid read of size 8
==6112==    at 0x400793: inserisci (lista.c:51)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc058 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== Invalid read of size 8
==6112==    at 0x4007B6: inserisci (lista.c:61)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc058 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== Invalid write of size 8
==6112==    at 0x4007BE: inserisci (lista.c:61)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc0a8 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
==6112== Invalid read of size 8
==6112==    at 0x4007C6: inserisci (lista.c:62)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc060 is not stack'd, malloc'd or (recently) free'd
==6112== 
==6112== Invalid write of size 8
==6112==    at 0x4007CE: inserisci (lista.c:62)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc0b0 is not stack'd, malloc'd or (recently) free'd
==6112== 
==6112== Invalid write of size 8
==6112==    at 0x4007DA: inserisci (lista.c:63)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc058 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== Invalid read of size 8
==6112==    at 0x4007E2: inserisci (lista.c:64)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc0a8 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
==6112== Invalid write of size 8
==6112==    at 0x4007EA: inserisci (lista.c:64)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x20 is not stack'd, malloc'd or (recently) free'd
==6112== 
==6112== 
==6112== Process terminating with default action of signal 11 (SIGSEGV)
==6112==  Access not within mapped region at address 0x20
==6112==    at 0x4007EA: inserisci (lista.c:64)
==6112==    by 0x40097B: main (main.c:39)
==6112==  If you believe this happened as a result of a stack
==6112==  overflow in your program's main thread (unlikely but
==6112==  possible), you can try to increase the size of the
==6112==  main thread stack using the --main-stacksize= flag.
==6112==  The main thread stack size used in this run was 8388608.
--6112-- REDIR: 0x4eb9df0 (free) redirected to 0x4c2bd80 (free)
==6112== 
==6112== HEAP SUMMARY:
==6112==     in use at exit: 16 bytes in 2 blocks
==6112==   total heap usage: 2 allocs, 0 frees, 16 bytes allocated
==6112== 
==6112== Searching for pointers to 2 not-freed blocks
==6112== Checked 78,464 bytes
==6112== 
==6112== LEAK SUMMARY:
==6112==    definitely lost: 0 bytes in 0 blocks
==6112==    indirectly lost: 0 bytes in 0 blocks
==6112==      possibly lost: 0 bytes in 0 blocks
==6112==    still reachable: 16 bytes in 2 blocks
==6112==         suppressed: 0 bytes in 0 blocks
==6112== Reachable blocks (those to which a pointer was found) are not shown.
==6112== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==6112== 
==6112== ERROR SUMMARY: 20 errors from 20 contexts (suppressed: 0 from 0)
==6112== 
==6112== 1 errors in context 1 of 20:
==6112== Invalid write of size 8
==6112==    at 0x4007EA: inserisci (lista.c:64)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x20 is not stack'd, malloc'd or (recently) free'd
==6112== 
==6112== 
==6112== 1 errors in context 2 of 20:
==6112== Invalid read of size 8
==6112==    at 0x4007E2: inserisci (lista.c:64)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc0a8 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
==6112== 
==6112== 1 errors in context 3 of 20:
==6112== Invalid write of size 8
==6112==    at 0x4007DA: inserisci (lista.c:63)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc058 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== 
==6112== 1 errors in context 4 of 20:
==6112== Invalid write of size 8
==6112==    at 0x4007CE: inserisci (lista.c:62)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc0b0 is not stack'd, malloc'd or (recently) free'd
==6112== 
==6112== 
==6112== 1 errors in context 5 of 20:
==6112== Invalid read of size 8
==6112==    at 0x4007C6: inserisci (lista.c:62)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc060 is not stack'd, malloc'd or (recently) free'd
==6112== 
==6112== 
==6112== 1 errors in context 6 of 20:
==6112== Invalid write of size 8
==6112==    at 0x4007BE: inserisci (lista.c:61)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc0a8 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
==6112== 
==6112== 1 errors in context 7 of 20:
==6112== Invalid read of size 8
==6112==    at 0x4007B6: inserisci (lista.c:61)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc058 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== 
==6112== 1 errors in context 8 of 20:
==6112== Invalid read of size 8
==6112==    at 0x400793: inserisci (lista.c:51)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc058 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== 
==6112== 1 errors in context 9 of 20:
==6112== Invalid read of size 4
==6112==    at 0x4006D0: crea_nodo (lista.c:36)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc098 is 0 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
==6112== 
==6112== 1 errors in context 10 of 20:
==6112== Invalid read of size 4
==6112==    at 0x4006C9: crea_nodo (lista.c:36)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc09c is 4 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
==6112== 
==6112== 1 errors in context 11 of 20:
==6112== Invalid write of size 4
==6112==    at 0x4006BE: crea_nodo (lista.c:35)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc0a0 is 8 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
==6112== 
==6112== 1 errors in context 12 of 20:
==6112== Invalid write of size 8
==6112==    at 0x4006B2: crea_nodo (lista.c:34)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc0b0 is not stack'd, malloc'd or (recently) free'd
==6112== 
==6112== 
==6112== 1 errors in context 13 of 20:
==6112== Invalid write of size 8
==6112==    at 0x4006A6: crea_nodo (lista.c:33)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc0a8 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
==6112== 
==6112== 1 errors in context 14 of 20:
==6112== Invalid write of size 8
==6112==    at 0x40069E: crea_nodo (lista.c:32)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112==  Address 0x51fc098 is 0 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x40097B: main (main.c:39)
==6112== 
==6112== 
==6112== 1 errors in context 15 of 20:
==6112== Invalid read of size 4
==6112==    at 0x4006D0: crea_nodo (lista.c:36)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112==  Address 0x51fc048 is 0 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== 
==6112== 1 errors in context 16 of 20:
==6112== Invalid read of size 4
==6112==    at 0x4006C9: crea_nodo (lista.c:36)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112==  Address 0x51fc04c is 4 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== 
==6112== 1 errors in context 17 of 20:
==6112== Invalid write of size 4
==6112==    at 0x4006BE: crea_nodo (lista.c:35)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112==  Address 0x51fc050 is 8 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== 
==6112== 1 errors in context 18 of 20:
==6112== Invalid write of size 8
==6112==    at 0x4006B2: crea_nodo (lista.c:34)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112==  Address 0x51fc060 is not stack'd, malloc'd or (recently) free'd
==6112== 
==6112== 
==6112== 1 errors in context 19 of 20:
==6112== Invalid write of size 8
==6112==    at 0x4006A6: crea_nodo (lista.c:33)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112==  Address 0x51fc058 is 16 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== 
==6112== 1 errors in context 20 of 20:
==6112== Invalid write of size 8
==6112==    at 0x40069E: crea_nodo (lista.c:32)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112==  Address 0x51fc048 is 0 bytes after a block of size 8 alloc'd
==6112==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6112==    by 0x400687: crea_nodo (lista.c:31)
==6112==    by 0x400729: inserisci (lista.c:42)
==6112==    by 0x400964: main (main.c:38)
==6112== 
==6112== ERROR SUMMARY: 20 errors from 20 contexts (suppressed: 0 from 0)
Errore di segmentazione (core dump creato)

我可能从valgrind的输出中写了太多东西,但我从来没用过它,说实话,我不是C的专家。

我明白,如果你不想检查所有这些东西,那有点无聊......但如果有人会帮助我,我会提前多多感谢他:)。

编辑aschepler:

  

新的valgrid错误:

Grandezza lista allocata 8
--2866-- REDIR: 0x4eb9750 (malloc) redirected to 0x4c2ab10 (malloc)
--[CREANODO]--// n.reparto 1 n_sintomo 3 gravita 3 codicecartella 1000
grandezza nodo allocato 8
--2866-- REDIR: 0x4ebfac0 (strlen) redirected to 0x4c2e0e0 (strlen)
Nuovo nodo creato e inizializzato, procedo con l'inserimento
lista vuota,metto in testa
--[CREANODO]--// n.reparto 1 n_sintomo 4 gravita 4 codicecartella 1001
grandezza nodo allocato 8
Nuovo nodo creato e inizializzato, procedo con l'inserimento
ELSE
==2866== Invalid write of size 8
==2866==    at 0x4007EA: inserisci (lista.c:64)
==2866==    by 0x40097B: main (main.c:39)
==2866==  Address 0x20 is not stack'd, malloc'd or (recently) free'd
==2866== 
==2866== 
==2866== Process terminating with default action of signal 11 (SIGSEGV)
==2866==  Access not within mapped region at address 0x20
==2866==    at 0x4007EA: inserisci (lista.c:64)
==2866==    by 0x40097B: main (main.c:39)
==2866==  If you believe this happened as a result of a stack
==2866==  overflow in your program's main thread (unlikely but
==2866==  possible), you can try to increase the size of the
==2866==  main thread stack using the --main-stacksize= flag.
==2866==  The main thread stack size used in this run was 8388608.
--2866-- REDIR: 0x4eb9df0 (free) redirected to 0x4c2bd80 (free)
==2866== 
==2866== HEAP SUMMARY:
==2866==     in use at exit: 80 bytes in 2 blocks
==2866==   total heap usage: 2 allocs, 0 frees, 80 bytes allocated
==2866== 
==2866== Searching for pointers to 2 not-freed blocks
==2866== Checked 78,496 bytes
==2866== 
==2866== LEAK SUMMARY:
==2866==    definitely lost: 0 bytes in 0 blocks
==2866==    indirectly lost: 0 bytes in 0 blocks
==2866==      possibly lost: 0 bytes in 0 blocks
==2866==    still reachable: 80 bytes in 2 blocks
==2866==         suppressed: 0 bytes in 0 blocks
==2866== Reachable blocks (those to which a pointer was found) are not shown.
==2866== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==2866== 
==2866== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==2866== 
==2866== 1 errors in context 1 of 1:
==2866== Invalid write of size 8
==2866==    at 0x4007EA: inserisci (lista.c:64)
==2866==    by 0x40097B: main (main.c:39)
==2866==  Address 0x20 is not stack'd, malloc'd or (recently) free'd
==2866== 
==2866== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Errore di segmentazione (core dump creato)

1 个答案:

答案 0 :(得分:1)

lista nodo=(lista)malloc(sizeof(nodo));

在此处创建名为nodo的变量,该变量将隐藏名为nodo的全局typedef。 sizeof运算符正在检查变量的大小,而不是类型,因此您只为指针分配足够的内存,对于struct来说不够。你需要

lista nodo=malloc(sizeof(struct _nodo));

lista nodo=malloc(sizeof(*nodo));

(不要投射malloc的结果。)

这解释了第一个valgrind错误。可能还有其他问题,但一次只能做一件事。