C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • malloc - Allocating Memory
  • calloc - Allocating and Initializing Memory
  • realloc - Resizing Memory
  • Memory Deallocation
  • Choosing the Right Function

Was this helpful?

  1. Dynamic Memory Allocation

malloc, calloc, and realloc

In C programming, memory management plays a crucial role in creating flexible and efficient programs. The malloc, calloc, and realloc functions are essential for dynamically allocating and resizing memory. This section explores the usage of these functions, their differences, and best practices.

malloc - Allocating Memory

  • The malloc function allocates a specified number of bytes of memory and returns a pointer to the beginning of the allocated block.

    int *dynamicArray = (int *)malloc(5 * sizeof(int));

This example allocates memory for an integer array with a size of 5.

calloc - Allocating and Initializing Memory

  • The calloc function allocates a specified number of blocks of memory, each with a specified size. It initializes the memory to zero.

    int *zeroedArray = (int *)calloc(5, sizeof(int));

Here, memory is allocated for an integer array of size 5, and each element is initialized to zero.

realloc - Resizing Memory

  • The realloc function is used to resize a previously allocated block of memory. It takes a pointer to the original block, the new size, and returns a pointer to the resized block.

    int *resizedArray = (int *)realloc(dynamicArray, 10 * sizeof(int));

This example resizes the previously allocated dynamicArray to accommodate 10 integers.

Memory Deallocation

  • Proper memory deallocation is essential to prevent memory leaks. The free function is used to release the memory allocated by malloc, calloc, or realloc.

    free(dynamicArray);
    free(zeroedArray);

Always free dynamically allocated memory when it is no longer needed.

Choosing the Right Function

  • Use malloc when you need to allocate a block of memory without initializing its content. Use calloc when you want to allocate and initialize memory to zero. Use realloc when you need to resize a previously allocated block.

Understanding these memory allocation functions and their appropriate use is crucial for effective memory management and avoiding common pitfalls in C programming.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousDynamic Memory AllocationNextProgram: User Input String

Was this helpful?