简体   繁体   English

Malloc在adt列表程序中返回null

[英]Malloc returns null in adt list program

I was working on a program with ADT list, queue, etc... Made by various .c and .h files. 我正在开发一个带有ADT列表,队列等的程序...由各种.c和.h文件制成。

I'm showing you a little extract of the program, just to let you understand better where is the problem; 我向您展示了该程序的一些摘录 ,目的只是为了让您更好地了解问题出在哪里。 this little program, however, is compilable and runnable. 但是,这个小程序是可编译且可运行的。

The problem is that a simple malloc in a function (something I've always did!), returns NULL, or causes segmentation fault (Visual Studio behavior is different than CodeBlocks for example). 问题在于函数中的简单malloc(我一直做过!)返回NULL或导致分段错误(例如,Visual Studio的行为不同于CodeBlocks)。 The malloc I'm talking about is the one in the InsertTop function. 我正在谈论的malloc是InsertTop函数中的那个。 Here is the short code, thanks for your help ! 这是短代码, 感谢您的帮助

#include <stdio.h>
#include <stdlib.h>

#define M 35+1

typedef struct student* Student;
struct student
{
    char name[M];
    char surname[M];
    char number[M];
};


typedef struct node* Node;
struct node
{
    Node prev;
    Student student;
    Node next;
};

typedef struct list* List;
struct list
{
    Node first;
    Node last;
};

/* PROTOTYPES */
List Initialize(List list);
List InsertTop(List list, Student student);
Student PromptStudent(Student student);

/* MAIN */
int main()
{
    /* DECLARATIONS */
    List list = NULL;
    Student student = NULL;

    /* LIST OPERATIONS */
    list=Initialize(list);
    while (0==0)
    {
        student=PromptStudent(student);
        list=InsertTop(list, student);
    }

    return EXIT_SUCCESS;
}

/* FUNCTIONS */
/* 1 */
List Initialize(List list)
{
    list = malloc(sizeof(List));

    list->first = list->last = NULL;

    return list;
}

/* 2 */
List InsertTop(List list, Student student)
{
    Node p;

    /* THIS LINE CREATES THE ERROR! */
    p = malloc(sizeof(Node));

    /* New node */
    p->prev = NULL;
    p->next = list->first;
    p->student = student;

    /* Update list with new node */
    if (list->first == NULL)
        list->first = list->last = p;
    else
    {
        list->first->prev = p;
        list->first = p;
    }

    printf(" -> Done!\n");

    return list;
}

/* 3 */
Student PromptStudent(Student student)
{
    student = malloc(sizeof(Student));

    printf("Name: "); scanf("%s", student->name);
    printf("Surname: "); scanf("%s", student->surname);
    printf("Number: "); scanf("%s", student->number);

    return student;
}

Thanks again! 再次感谢!

Node is a pointer type. Node是指针类型。 You want to allocate as much memory to a pointer as of the type it is pointing to. 您要为指针分配与其指向的类型一样多的内存。 This however isn't Node , but struct node or simply what p is pointing to, that is *p . 但是,这不是Node ,而是struct node或者只是p指向的对象,即*p

This 这个

 p = malloc(sizeof(Node));

shall be 应该

 p = malloc(sizeof (struct node));

or even better 甚至更好

 p = malloc(sizeof *p);

As a general rule: Do not typedef pointers, this very easily leads to confusion, as here. 通常的规则是:请勿typedef指针,这很容易导致混乱,如此处所示。


Some other issues with your code: 您的代码还有其他一些问题:

  • This #define M 35+1 shall be #define M (35+1) . 这个#define M 35+1应该是#define M (35+1)
  • Instead of while (0==0) you can do while (1) . 代替while (0==0)可以执行while (1)
  • scanf("%s" ... shall be scanf("%35s" ... to avoid buffer overflows, if the user enters more then 35 char s. scanf("%s" ...应该是scanf("%35s" ...以避免缓冲区溢出,如果用户输入的char超过35个s。

Make below suggested changes: Memory should be allocated for sizeof structure as shown below and not for size of pointer to a structure.This needs to be updated in all the malloc() in your program. 进行以下建议的更改:如下所示,应为sizeof结构分配内存,而不是为结构指针分配大小。需要在程序中的所有malloc()中进行更新。

   list = malloc(sizeof(struct list));

   p = malloc(sizeof(struct node));

   student = malloc(sizeof(struct student));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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