首页 文章

为作业创建自己的malloc . 获得分段错误

提问于
浏览
-2

分段错误发生在注释点 . 我认为这与我没有初始化头部和尾部节点这一事实有关 . 我也尝试初始化为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("here1\n");

void *my_bestfit_malloc(int size)
{
    Node* newNode = NULL;
    printf("here2\n");
    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("here3\n");
        tail->previous = head;
        newNode->size = size;
        newNode->status = 1;
        first++;
    }
    else
    {
        Node* currNode = head->next;
        printf("here4\n");
        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("here5\n");
                break;
            }
            else
            {
                currNode = currNode->next;
                printf("here6\n");
            }
        }
        if(currNode->next == tail)
        {
            newNode = sbrk(size + sizeof(Node));
            HEAP_END = (int*)sbrk(0);
            newNode->size = size;
            newNode->status = 1;
            printf("here7\n");
        }
    }
    return newNode + sizeof(Node);
}

 int main()
{
    typedef struct person{
        int age;
        char sex;
    }person;
    printf("main1\n");
    person* dave = (person*)my_bestfit_malloc(sizeof(person));
    printf("main2\n");
    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:\n\tAge: %d\n\tSex: %c\n", dave->age, dave->sex);
    printf("Vicki:\n\tAge: %d\n\tSex: %c\n", dave->age, dave->sex);
    printf("Alex:\n\tAge: %d\n\tSex: %c\n", dave->age, dave->sex);
}

所以我尝试将我的Node * head和tail改为:Node head;节尾;相反,但收到这些错误:

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;相反,但我不明白最后两个 .

最终编辑:如果想通了 . head和tail的指针需要是实际的Node结构而不是struct指针 . 我还需要返回一个void指针而不是Node .

1 回答

  • 0

    看起来你正在为头部分配值 . 头部尚未分配 .

    更改:

    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("here1\n");
    
    void *my_bestfit_malloc(int size)
    {
        Node* newNode = NULL;
        printf("here2\n");
        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("here3\n");
            tail.previous = &head;
            newNode->size = size;
            newNode->status = 1;
            first++;
        }
        else
        {
            Node* currNode = head.next;
            printf("here4\n");
            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("here5\n");
                    break;
                }
                else
                {
                    currNode = currNode->next;
                    printf("here6\n");
                }
            }
            if(currNode->next == &tail)
            {
                newNode = sbrk(size + sizeof(Node));
                HEAP_END = (int*)sbrk(0);
                newNode->size = size;
                newNode->status = 1;
                printf("here7\n");
            }
        }
        return newNode + sizeof(Node);
    }
    
     int main()
    {
        typedef struct person{
            int age;
            char sex;
        }person;
        printf("main1\n");
        person* dave = (person*)my_bestfit_malloc(sizeof(person));
        printf("main2\n");
        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:\n\tAge: %d\n\tSex: %c\n", dave->age, dave->sex);
        //printf("Vicki:\n\tAge: %d\n\tSex: %c\n", dave->age, dave->sex);
        //printf("Alex:\n\tAge: %d\n\tSex: %c\n", dave->age, dave->sex);
    }
    

    现在运行代码它最少提供以下输出:

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

相关问题