Hi everyone well currently I am working on a simple malloc and free implementation but I am facing some problems . I get a segmentation fault this is my output

inserting node 0 0xc87000
inserting node 1 0xc87014
inserting node 2 0xc87028
inserting node 3 0xc8703c
inserting node 4 0xc87050
inserting node 5 0xc87064
inserting node 6 0xc87078
inserting node 7 0xc8708c
inserting node 8 0xc870a0
inserting node 9 0xc870b4
The list head : 0xc87000 inside the del_search
returned value by the del_search 0xc87050
back 0x400c8703c
Segmentation fault (core dumped)

here is the code

typedef struct mem_chunk *ptr ;
int i=0;
ptr   list_head =NULL ; // a pointer to the head of the list that will be used to link  used  chunks
typedef struct mem_chunk
{
size_t     size;
ptr        next;
ptr        prev;
int        flag;//if flag=1 then this chunk is free else if flag=0 then this chunk is used
void*   mem_brk;
};
ptr  del_search (void* node)
{
ptr temp;
temp=list_head;
printf("The list head : %p  inside the del_search \n",temp);
    while(temp != NULL )
    {
        if (temp->mem_brk==node)
            {
                printf("returned value by the del_search %p \n",temp);
                return temp;
            }
        else
            temp=temp->next;
    }

return (ptr)-1;
}
int delete(void* node)// return 0 on success and -1 on failure 
// currently not fully implemented still there are some cases not implemented 
{
    if (list_head==NULL) // when deleting from an empty list
    return -1 ;
    else
    {
        ptr temp;

        temp=del_search(node);

        if(temp ==(void*)-1) // the pointer was given by malloc
        return -1;
        else
        {
            ptr back , next;
            next=temp->next;

            temp->next->prev=temp->prev;
            back=temp->prev;
            printf("back %p\n",back);
            void* brk=back->mem_brk;

            back->next=next; // this what caused the segmentation fault             
            temp->flag=1;// making the node available
            return 0;

        }
    return -1;   // the pointer wasn't give by malloc
    }

}

void free(void* brk)
{
if (brk==NULL)
return;
int temp;
temp=delete(brk);


if (temp==(ptr)-1)
return;

}
void main(){
ptr temp[10];


// printf("sbrk 0 %p \n",sbrk(0));
int i;
for(i=0 ;i<10 ;i++)
    {
        temp[i]=malloc(4); // malloc works properly also the insert function

    }   


free(temp[4]->mem_brk);


}

the problem is that the value of the pointer changes and I don't know why. so can anyone help ??

First off, you should always use int as the type for main(), not void. While some compilers allow for other return types, the only really correct (in the sense of portable and matching the standard) declaration is int main() (or, when using command-line arguments, int main(int argc, char* argv[])).

Second, you don't #include the necessary headers, at least not in the sample of code you have given us. This is important because you also re-declare a function free() which is significantly different from the Standard C free() declared in <stdlib.h>. If you had included the headers, your com[piler should have given a multiple declaration error.

Third, the argument passed to malloc() is simply erroneous; you are allocating what you assume to be the size of a pointer (though it would actually depend on the system and compiler), but are returning it to the elements of an array of pointers to struct mem_chunk, which is significantly larger than 4 bytes long. Assuming you intended to populate the array with struct mem_chunk structures, the correct call should be:

 temp[i]=malloc(sizeof(struct mem_chunk));

Even if your intent was to populate the array with pointers to pointers (which is not what it is declared as), you would need to call it as

  temp[i]=malloc(sizeof(ptr));

because the size of a pointer will vary depending on system and the compiler.

In any case, you aren't actually creating a linked list at any point in the program; instead, you are creating an array of struct mem_chunk pointers. You mention having an insert() function, but didn't post it; could you do so now, so we can see what is going on with the insertion?

here is the insert function I will consider your notes but what is the problem with my code??

#include<sys/types.h>
#include<unistd.h>

typedef struct mem_chunk *ptr ;

typedef struct mem_chunk
{
size_t     size;
ptr        next;
ptr        prev;
int        flag;//if flag=1 then this chunk is free else if flag=0 then this chunk is used
void*   mem_brk;
};

// Variables declaration        //
ptr   list_head =NULL ; // a pointer to the head of the list that will be used to link  used  chunks
ptr   list_tail=NULL; // a pointer to the tail of the list that will be used to link  used  chunks

ptr freeL_head=NULL; // for free chunks
ptr freeL_tail=NULL;//for free chunks
const size_t over_head=16;
// Functions 
int i=0;
void insert (ptr new_node)
{

    if(list_head==NULL) // when the list is empty
    {
        printf("inserting node %d   %p \n ",i,new_node); i++;
        list_head =new_node;
        list_tail =new_node;
        new_node->next=NULL;
        new_node->prev=NULL;
    }
    else
    {   printf("inserting node %d   %p \n ",i,new_node);i++;
        list_tail->next=new_node;
        new_node->prev=list_tail;
        new_node->next=NULL;
        list_tail=new_node;     
    }

}

here is the insert function I will consider your notes but what is the problem with my code??

You tell us what the problem is. Reading code is hard enough without searching for some unknown error.

I post the output the problem is that the value of the pointer changes after it is passed
to delete there's "40" which wasn't there

0x400c8703c
the address should be 0c8703c

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.