Answer: Let’s try out following programs, first,
/* compiletime_vs_runtime1.c -- program differentiates between two */ #include <stdio.h> int main() { float marks[10]; int i; puts("user, enter marks of 10 students..."); for (i = 0; i < 10; i++) scanf("%f", &marks[i]); puts("Marks of 10 students are:"); for (i = 0; i < 10; i++) printf("%.2f ", marks[i]); puts(""); return 0; }
Output of the above program as,
user, enter marks of 10 students... 54 2 1 34 67 86 78 34 98 78 Marks of 10 students are: 54.00 2.00 1.00 34.00 67.00 86.00 78.00 34.00 98.00 78.00
What do you observe here as a limiting factor? You can process marks of up to max of 10 students. What about if you wished to process less than 10 or in a case more than 10 marks, say, marks of 100 students or 5 marks or even there’s a situation where you are not knowing in advance, how many?
O key, for sure, you declare an array with largest possible size, say 10000 elements size or larger than this, thinking program can now process any number of marks. But, still, there’s always a possibility of overflowing of array. And further worse to it, if just few marks to process, there’s a huge memory wastage. Actually, array size is a constant literal and compiler allocates, while compiling the program, enough memory to hold as much as elements defined by that constant.
Let’s take up with the case where we are not known in advance how many elements to process, till run time, say, 40 or 400 or just 5 or some other value. This could be solved if we would be able to write a program that asks user, at run time, how many elements to process, then allocates memory for specified no. of elements. No wastage of memory and program processes the desired no. of elements. Let’s try out such a case,
/* compiletime_vs_runtime2.c -- program differentiates between two */ #include <stdio.h> int main() { int n, i; float marks[n]; printf("How many marks to process? Enter a positive integer...\n"); scanf("%d", &n); printf("now enter marks of students...\n"); for (i = 0; i < n; i++) scanf("%f", &marks[i]); printf("Processing %d Marks.\n", n); return 0; }
Let’s consider the output, for some arbitrary no.,
How many marks to process? Enter a positive integer... 5 now enter marks of students... 32 5.4 67.78 23.56 89 Processing 5 Marks.
So, what you observed here? Compiler didn’t allocate memory at compile time. Instead, program asked user, at runtime, no. of marks to process and allocated them memory and processed them. This technique is run time allocation. Though, run time allocation is efficient but program retained memory until it exited.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Buy C Books
- Buy Computer Science Books
- Apply for Computer Science Internship