创建工作分配自己的malloc。获取分割故障 [英] Creating own malloc for an assignment. Getting a segmentation fault

查看:211
本文介绍了创建工作分配自己的malloc。获取分割故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

分割故障发生在该注释点。我认为这与事实,我没有初始化的头和尾节点做。我试着初始化为NULL,以及并没有奏效。不幸的是,我真的不知道如何在不使用malloc初始化。任何帮助将是巨大的。谢谢你。

The segmentation fault occurs at the point with the comment. I think it has to do with the fact that I'm not initializing the head and tail Nodes. I've tried to initialize the to NULL as well and that didn't work. Unfortunately, I don't really know how to initialize them without using malloc. Any help would be great. Thanks.

#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);
}

所以,我想改变我的节点*头尾:节点头;节点尾;相反,却收到以下错误:

So I tried changing my Node* head and tail to: Node head; Node tail; instead, but received these errors:

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 =尾;代替,但我不明白的最后两个。

I understand the first three, I need to use head.next = tail; instead, but I don't understand the last two.

最后编辑:
如果得到想通了。头部和尾部的指针需要真实节点结构,而不是结构指针。此外,我需要返回一个空指针而不是一个节点。

Final edit: Got if figured out. The pointers for head and tail need to be actual Node structs instead of struct pointers. Also I needed to return a void pointer instead of a Node.

推荐答案

看起来像你赋值头。头尚未分配。

Looks like you are assigning values to head. head hasn't been allocated.

变化:

Node* head;
Node* tail;

Node head;
Node tail;

和访问指针结构使用的一员时 - &GT; 。如流浆&GT;大小
和访问结构使用的一员的时候。 head.size

我做了一些修改code,现在的计划至少可以分配和使用的第一个。但是随后的类型分配失败。我猜想也许无效* my_bestfit_malloc(INT大小)有一些逻辑/指针问题....

I made some changes to your code and now the program at least can allocate the and use the first person. However subsequent allocations of type person fails. I suspect perhaps void *my_bestfit_malloc(int size) has some logic/pointer issues....

#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);
}

运行code现在它至少提供了以下的输出:

Running the code now it least gives the following output:

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

这篇关于创建工作分配自己的malloc。获取分割故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆