"Education is the best friend. An educated person is respected everywhere. Education beats the beauty and the youth." Chanakya (Indian politician, strategist and writer, 350 BC-275 BC)
 

Main Menu

Home
Articles
SVTechie Blog
Links
Download
Discussion Forum
Photo Gallery
Quick Bites
FAQs

Login






Lost Password?
No account yet? Register

Statistics

We have 2 guests online

SVTechie Recommends


powered_by.png, 1 kB

Text Links


Home
Dynamic Memory Allocation Performance PDF Print E-mail
Written by SVTechie   
Sunday, 09 April 2006
Article Index
Dynamic Memory Allocation Performance
Quick Notes on Memory Allocation
ANSI C Linked List Code
C Linked List Code
Record Collapsing
Pool Based Library Implementation
Conclusion
 

 

 

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

}

 



Last Updated ( Saturday, 06 May 2006 )
 
Next >
Secured Loans | Remortgages | Online Loans | Flights | MPAA
© 2008 SVTechie :: Online Resources For Techies BY Techies
Joomla! is Free Software released under the GNU/GPL License.