Difference between Compile Time and Run Time Memory Allocation in C

Question: What is Difference Between Compile-Time and Run-Time Memory Allocation in C Programming?

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]);
    return 0;

Output of the above program as,

user, enter marks of 10 students...
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.,

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
How many marks to process? Enter a positive integer...
now enter marks of students...
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.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.