C语言 为作业创建自己的 malloc.出现分段错误



分段错误发生在带有注释的点。我认为这与我没有初始化头节点和尾节点的事实有关。我也尝试将初始化为 NULL,但没有奏效。不幸的是,我真的不知道如何在不使用malloc的情况下初始化它们。任何帮助都会很棒。谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//the structure of the node in the linked list
typedef struct Node{
    int size;
    int status;
    struct Node* next;
    struct Node* previous;
}Node;
int* HEAP_START = 0;
int* HEAP_END = 0;
Node* head;
Node* tail;
int first = 0;
//printf("here1n");
void *my_bestfit_malloc(int size)
{
    Node* newNode = NULL;
    printf("here2n");
    if(first == 0)
    {
        HEAP_START = (int*)sbrk(0);
        newNode = sbrk(size + sizeof(Node));
        HEAP_END = (int*)sbrk(0);
        head->next = tail;      //segmentation error happens here
        printf("here3n");
        tail->previous = head;
        newNode->size = size;
        newNode->status = 1;
        first++;
    }
    else
    {
        Node* currNode = head->next;
        printf("here4n");
        while(currNode->next != tail)
        {
            if(currNode->size == size)
            {
                newNode = currNode;
                currNode->previous->next = currNode->next;
                currNode->next->previous = currNode->previous;
                newNode->size = size;
                newNode->status = 1;
                printf("here5n");
                break;
            }
            else
            {
                currNode = currNode->next;
                printf("here6n");
            }
        }
        if(currNode->next == tail)
        {
            newNode = sbrk(size + sizeof(Node));
            HEAP_END = (int*)sbrk(0);
            newNode->size = size;
            newNode->status = 1;
            printf("here7n");
        }
    }
    return newNode + sizeof(Node);
}
 int main()
{
    typedef struct person{
        int age;
        char sex;
    }person;
    printf("main1n");
    person* dave = (person*)my_bestfit_malloc(sizeof(person));
    printf("main2n");
    person* vicki = (person*)my_bestfit_malloc(sizeof(person));
    printf("main3");
    person* alex = (person*)my_bestfit_malloc(sizeof(person));
    dave->age = 26;
    dave->sex = 'M';
    vicki->age = 24;
    vicki->sex = 'F';
    alex->age = 19;
    alex->sex = 'F';
    printf("Dave:ntAge: %dntSex: %cn", dave->age, dave->sex);
    printf("Vicki:ntAge: %dntSex: %cn", dave->age, dave->sex);
    printf("Alex:ntAge: %dntSex: %cn", dave->age, dave->sex);
}

所以我尝试将我的节点*头和尾部更改为:节点头;节点尾部;相反,但收到以下错误:

mymalloc.c: In function ‘my_bestfit_malloc’:
mymalloc.c:38: error: invalid type argument of ‘->’ (have ‘Node’)
mymalloc.c:40: error: invalid type argument of ‘->’ (have ‘Node’)
mymalloc.c:47: error: invalid type argument of ‘->’ (have ‘Node’)
mymalloc.c:49: error: invalid operands to binary != (have ‘struct Node *’     and ‘Node’)
mymalloc.c:67: error: invalid operands to binary == (have ‘struct Node *’     and ‘Node’)

我理解前三个,我需要使用 head.next = tail;相反,但我不理解最后两个。

最终编辑:如果弄清楚了就知道了。头和尾的指针必须是实际的 Node 结构,而不是结构指针。此外,我还需要返回一个空指针而不是节点。

看起来您正在为头部分配值。 头尚未分配。

改变:

Node* head;
Node* tail;

自:

Node head;
Node tail;

当访问指向struct use ->的指针的成员时。如head->size当访问struct use .的成员时,例如 head.size .

我对你的代码做了一些更改,现在程序至少可以分配和使用第一个person。但是,类型 person 的后续分配将失败。我怀疑也许void *my_bestfit_malloc(int size)有一些逻辑/指针问题......

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//the structure of the node in the linked list
typedef struct Node{
    int size;
    int status;
    struct Node* next;
    struct Node* previous;
}Node;
int* HEAP_START = 0;
int* HEAP_END = 0;
struct Node head;
struct Node tail;
int first = 0;
//printf("here1n");
void *my_bestfit_malloc(int size)
{
    Node* newNode = NULL;
    printf("here2n");
    if(first == 0)
    {
        HEAP_START = (int*)sbrk(0);
        newNode = sbrk(size + sizeof(Node));
        HEAP_END = (int*)sbrk(0);
        head.next = &tail;      //segmentation error happens here
        printf("here3n");
        tail.previous = &head;
        newNode->size = size;
        newNode->status = 1;
        first++;
    }
    else
    {
        Node* currNode = head.next;
        printf("here4n");
        while( currNode != &tail)
        {
            if(currNode->size == size)
            {
                newNode = currNode;
                currNode->previous->next = currNode->next;
                currNode->next->previous = currNode->previous;
                newNode->size = size;
                newNode->status = 1;
                printf("here5n");
                break;
            }
            else
            {
                currNode = currNode->next;
                printf("here6n");
            }
        }
        if(currNode->next == &tail)
        {
            newNode = sbrk(size + sizeof(Node));
            HEAP_END = (int*)sbrk(0);
            newNode->size = size;
            newNode->status = 1;
            printf("here7n");
        }
    }
    return newNode + sizeof(Node);
}
 int main()
{
    typedef struct person{
        int age;
        char sex;
    }person;
    printf("main1n");
    person* dave = (person*)my_bestfit_malloc(sizeof(person));
    printf("main2n");
    person* vicki = (person*)my_bestfit_malloc(sizeof(person));
    printf("main3");
    person* alex = (person*)my_bestfit_malloc(sizeof(person));
    dave->age = 26;
    dave->sex = 'M';
    //vicki->age = 24;
    //vicki->sex = 'F';
    //alex->age = 19;
    //alex->sex = 'F';
    printf("Dave:ntAge: %dntSex: %cn", dave->age, dave->sex);
    //printf("Vicki:ntAge: %dntSex: %cn", dave->age, dave->sex);
    //printf("Alex:ntAge: %dntSex: %cn", dave->age, dave->sex);
}

现在运行代码,它至少会给出以下输出:

jrn@VirtualBox-mint17 ~ $ ./a.out 
main1
here2
here3
main2
here2
here4
main3here2
here4
Dave:
    Age: 26
    Sex: M

相关内容

  • 没有找到相关文章