Dynamic Memory Allocation Performance

 

 

This implementation is very simple Linked List Implementation. A single link list is created with  same number of nodes as number of datasets, resulting in large number of allocation and deallocation requests. Following is the code snippet for this algorithm.

 

 ANSI C Linked List Implementation
 

#include <stdio.h>

typedef struct nodedef {
        int data;
        struct nodedef *next;
        } node;

void main () {
  node *str, *tmp, *cur;
  int i;

  str = cur = NULL;
  for (i = 0; i < NUM_REQUEST; i++) {  

    // Create New Node   

    tmp = (node *) malloc (sizeof(node));

    tmp->data = i;    

    tmp->next = NULL;   

    // Insert in the list   

    if (str == NULL) {     

       // First Node     

       str = tmp;   

    } else {     

        // Insert at End (pointed by cur)     

        cur->next = tmp;
    }
    cur = tmp;
  }

  // Deallocate each node
  tmp = str;
  for (cur = str->next; cur != NULL; cur = cur->next) {
    free (tmp);
    tmp = cur;
  }

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *