|
Dynamic Memory Allocation Performance |
|
|
|
|
Written by SVTechie
|
|
Sunday, 09 April 2006 |
|
Page 4 of 7
This implementation is again, very simple Linked List Implementation in C++. Code structure is same as before except malloc is replaced with new and free is replaced with delete. Following is the code snippet for this algorithm.
C++ Based Link List Implementation
|
#include <new>
typedef struct nodedef {
int data;
struct nodedef *next;
} node;
int 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 = new 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);
delete tmp;
tmp = cur;
}
} |
|
|
Last Updated ( Saturday, 06 May 2006 )
|