Dynamic Memory Allocation Performance

 

 

 

This implementation is very simple array based Linked List Implementation in ANSI C. A node can hold more than 1 dataset. In this example, an array of 100 elements is created in each node. This reduces allocation/de-allocations requests.

 Array Based Memory Allocation

#include <stdio.h>

typedef struct nodedef {
        int data[100];

        struct nodedef *next;
        } node;

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

  str = cur = NULL;
  for (i = 0, j = 0; i < NUM_REQUEST; i++, j=i%100) {

    // Create New Node
    if (j == 0) {
      tmp = (node *) malloc (sizeof(node));
      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;
    }
    tmp->data[j] = i;
  }

  // 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 *