Answer: Static memory allocation is a compile time constant i.e. memory is allocated during compilation of the program. For example,
int main(void) { int fours[10]; /* fours is an array of 10 integers */ char symbols[128]; /* symbols an array of 128 characters */ return 0; }
When above program is compiled, fours is allocated (10 * sizeof(int)) bytes, symbols is allocated 128 bytes. Here memory is allocated during compile time. We call this static allocation because memory is allocated once at the compile time and we can’t request for reallocation if need arises. Further, we can’t free up memory once we are done with this. Memory allocated statically frees up only when program exits.
There are situations where we are not sure of exact amount of memory we require until run time, during the entire execution of program. For example, to keep record of foreigners visiting to India every year. In such situations, if we allocate memory statically presuming some maximum number, there might be wastage of memory if far fewer people visited the country in a particular year as well as there’s always
a possibility to overflow.
What here we require is some scheme to allocate memory as per ours’ need. For example, if we need to find average of marks of all students of all classes. Students might count differently in different classes. In such situations, we require dynamic memory allocation.
In systems, functions malloc() and free() maintain pool of available memory. When, memory, in bytes, is requested, malloc() attempts to allocate memory from this pool. If enough memory is not there, malloc() requests O.S. to allocate. O.S. tries to free up memory and returns the same to the pool, but if O.S., too, fails, malloc() returns NULL. For example,
/* create_sll.c -- program creates a singly linked list */ #include <stdio.h> #include <stdlib.h> #define INSERT 1 #define QUIT 2 /* structure declaration */ typedef struct NODE { struct NODE *link; int value; } Node; void insert_sll(Node **, const int); int main(void) { Node *root = 0; /* root is NULL */ Node **p2r = &root; /* pointer-to-root */ int value; int op; puts("\n**Let's create a Singly Linked List**\n"); printf("User, enter 1 for INSERT and 2 for QUIT : "); while (1) { while (scanf("%d", &op) == 1 && (op == INSERT || op == QUIT )) { if (op == INSERT) { printf("User, enter an integer value: "); scanf("%d", &value); insert_sll(p2r, value); } else if (op == QUIT) { printf("Thank You!\n"); return 0; } printf("\nWant to insert more integer values,\nenter 1 " "for INSERT, else 2 for QUIT : "); } puts("Entered is a WRONG choice, enter 1 for " "INSERT, 2 for QUIT"); } } void insert_sll(Node **linkp, const int value) { Node *current = 0; Node *newnode = 0; /* Let's create an ordered singly linked list */ /* firstly, ensure if value isn't already in the list */ /* if value is already in the list, we won't add duplicate */ while ((current = *linkp) != NULL && current->value < value) linkp = ¤t->link; /* if value is already in the list */ if (current != NULL && current->value == value) { printf("\n\aValue %d is already in the list.\n", value); return ; } /* value isn't already in the list */ /* Let's allocate memory to newnde */ newnode = (Node *)malloc(sizeof(Node)); /* If memory allocated to newnode successfully */ if (newnode == NULL) { printf("Not sufficient Memory!\n"); exit(EXIT_FAILURE); } /* write in value in value field of newnode */ newnode->value = value; /* insert newnode in the list */ /* between current and previous */ newnode->link = current; *linkp = newnode; }
Notice that in above program, we allocate memory to newnode when a new value is required to be inserted in the list. Since, we, in advance, are not sure of how big the List is i.e. how many values might comprise the list, we, therefore, dynamically allocate memory as and when needs. NULL is a visual representation for Null Pointer. Internally, It’s 0, zero. Though, program doesn’t include code fragment to delete a value from the list, we can, however, write a function to delete a particular value from the list, and back the freed up memory to the pool of available memory. The advantage of dynamic allocation.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice BCA MCQs
- Practice Computer Science MCQs
- Check Computer Science Books
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship