hi everyone I'm working currently with linked list , and there's a problem with my code , the link is writing the same place every time (it replaces the previous values with the new ones without inserting a new one ) can anyone help ? Thanks in advance


struct mem_chunk 
unsigned   *mem_break ;    
int size ;          
int  required_size; 
struct mem_chunk * next;


struct mem_chunk  *used_chunks , *unused_chunks;

void insert( struct mem_chunk new_node , int flag)

    if ( flag ==0 )

        if (used_chunks == NULL )

            used_chunks = &new_node; 


        else {

            struct mem_chunk *current ; 
            current = used_chunks;

6 Years
Discussion Span
Last Post by Ancient Dragon

The first parameter to that function needs to be a pointer so that it can be added to the list. What you are doing now is just setting a pointer to a local variable, which goes out of scope as soon as that function returns. You should ALWAYS pass structures by reference (pointers), never by value.
That function might look like this:

void insert( struct mem_chunkI* new_node , int flag)
    if( used_chucks == NULL)
       used_chuncks = new_node;

In the else statement, if you want to add the new node to the head of the linked list

new_node->next = used_chunck;
used_chunck = new_node;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.