简体   繁体   中英

malloc doesn't work for second element of list

The function should create a linked list of structs, I've seen in the debugger that it allocates the memory for the first element, but the program crashes during the allocation of memory for the second element.

struct Tipologia{
    int mq;
    int prezzo;
    int n;
    char descrizione[100];
};

struct Nodo{
    struct Tipologia t;
    struct Nodo *next;
};

typedef struct Nodo* NODO;

struct Tipologia creaTipologia(){
    struct Tipologia t;
    printf("MQ?\n");
    scanf("%d", &(t.mq));
    printf("PREZZO?\n");
    scanf("%d", &(t.prezzo));
    printf("DISPONIBILI?\n");
    scanf("%d", &(t.n));
    printf("DESCRIZIONE?\n");
    int c;
    while ( (c = getc(stdin)) != EOF && c != '\n');
    fgets((t.descrizione), 100, stdin);
    return t;
}

NODO costruisciPalazzo(){
    int continua;
    NODO h= NULL;
    printf("VUOI COSTRUIRE UN APPARTAMENTO? 1 SI, 0 NO\n");
    scanf("%d", &continua);
    if(continua){
        NODO n= malloc(sizeof(NODO));
        h= n;
        n->t= creaTipologia();
        n->next= NULL;
        printf("VUOI COSTRUIRE UN APPARTAMENTO? 1 SI, 0 NO\n");
        scanf("%d", &continua);
        while(continua){
            NODO nodo= malloc(sizeof(NODO));
            n->next= nodo;
            n= nodo;
            n->t= creaTipologia();
            printf("VUOI COSTRUIRE UN APPARTAMENTO? 1 SI, 0 NO\n");
            scanf("%d", &continua);
        }
        n->next= NULL;
    }
    return h;
}

I've been following the instructions of my professor but it keeps on crashing without giving any error to explain what actually happens. It seems to work for my classmates and we can't figure out what the problem is. Please help

The problem is that you are using a typedef that hides the fact that NODO is a pointer. This is bad, as it creates confusing situations where you are expecting a type to have a certain size and be used with a certain syntax, but in effect it's something entirely different.

For instance, you have to do this:

h= n;
n->t= creaTipologia();
n->next= NULL;

It's confusing that both h and n are not explicitly declared as pointers, but you have to use the arrow notation.

My suggestion is that you either remove completely the typedef and use struct nodo in your code, or at least remove the pointer from the typedef . Do the same with the other structure for consistency:

typedef struct {
  int mq;
  int prezzo;
  int n;
  char descrizione[100];
} TIPOLOGIA;

typedef struct Nodo {
  TIPOLOGIA t;
  struct Nodo *next;
} NODO;

You can also simplify your malloc using the object as a reference for its size. For instance:

NODO *h = malloc(sizeof *h);

This avoids the need of specifying the type of h when calling malloc . It's better for reuse too.

通过执行malloc(sizeof(struct Nodo))而不是malloc(sizeof(NODO))即可解决此问题,因为使用NODO它将仅分配指针,而使用struct Nodo则分配整个元素

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM