Creating own malloc for an assignment. Getting a segmentation fault

烂漫一生 提交于 2019-12-14 03:35:37

问题


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’)

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.


回答1:


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

change:

Node* head;
Node* tail;

to:

Node head;
Node tail;

And when accessing a member of pointer to a struct use ->. Such as head->size And when accessing a member of a struct use . Such as head.size.

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

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


来源:https://stackoverflow.com/questions/29187873/creating-own-malloc-for-an-assignment-getting-a-segmentation-fault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!