Static and Dynamic Memory

Previous
Next

Static v/s Dynamic memory:

  • In C application, we can allocate the memory to store the information in 2 ways
    • Static – fixed size
    • Dynamic – size varies

Static memory:

  • All primitive data types having fixed size of memory.
  • Using primitive variables we can allocate fixed memory blocks to store the data.
  • Derived data types are Array and String
  • User defined data types are Structure and Union
  • We can allocate the memory to derived types and user defined types in 2 ways.

Static allocation:

  • Array can store more than 1 element but of same data type.
  • String stores only characters as elements.
  • Structure can store different types of data like Emp records, Student records, Account records and so on.

Dynamic memory:

  • Once we allocate the memory, we can increase or decrease the size of memory block depends on requirement.
  • We can allocate memory using pointers.
  • Pointer can store a memory block address of any size.

When Static memory allocates?

  • At Compile time (completely wrong)
  • At runtime – while programming is running.

When Dynamic memory allocates?

  • At runtime – while programming is running.

Who will allocate Static memory?

Every application run by OS.

OS allocate required memory to execute the application.

What is Compiler?

  • It is a translator(program)
  • It will check whether the program is syntactically correct or not.
  • If the program follows syntax rules, it converts the source code into machine code.

Note that, compiler is not responsible for allocating the memory.

How can we allocate memory dynamically in C language?

  • Using the functionality of stdlib.h header file.
  • Stdlib.h header file is providing 4 standard methods to allocate and de-allocate the memory.
    • Malloc()
    • Calloc()
    • Realloc()
    • Free()
Previous
Next

Add Comment

Courses Enquiry Form