DMA Assignments in C
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

DMA Dynamic Memory Allocation Assignments in C with Solutions

📑 On this page:
  • What is Dynamic Memory Allocation?
  • The 9 Practice Assignments
  • How to Approach Each Assignment
  • Frequently Asked Questions
📚 In this section, you will practice:
  • Allocating memory at runtime using malloc() and calloc()
  • Resizing allocated blocks with realloc()
  • Releasing memory back to the OS using free()
  • Building common data structures (arrays, strings, linked lists, stacks, queues, trees, graphs) with dynamic memory
  • Writing leak-free, production-style C code

What is Dynamic Memory Allocation?

Dynamic Memory Allocation (DMA) is the process of allocating memory at runtime rather than at compile time. In C, DMA is managed through four standard library functions from <stdlib.h>: malloc(), calloc(), realloc(), and free().

DMA is essential when the size of data is not known in advance — for example, when reading an arbitrary number of records from a file, building a data structure of unknown length, or creating linked structures like lists and trees.

💡 Quick Definition: DMA gives your program the power to ask the operating system for memory exactly when it needs it, and to return that memory when it's done.

Dynamic memory allocation DMA practical assignments and solutions in C programming language

The 9 Practice Assignments

Each assignment below is a full working problem, explained step-by-step with a complete C program and output. Work through them in order — the difficulty increases gradually.

# Assignment Key Function
1Dynamically allocate memory for an array of integers and display the sum of all elementsmalloc
2Dynamically allocate memory for a string and reverse the string in placemalloc
3Dynamically allocate a 2D array and compute the transpose of the matrixmalloc
4Dynamically allocate a linked list and implement a function to reverse itmalloc
5Dynamically allocate a stack and perform push and pop operationsmalloc
6Dynamically allocate a binary search tree and implement insert and searchmalloc
7Dynamically allocate a queue and implement enqueue and dequeuemalloc
8Dynamically allocate a graph and implement add-vertex and add-edgemalloc
9Dynamically allocate a priority queue and implement insert and extract-by-prioritymalloc

How to Approach Each Assignment

Every DMA program in this section follows the same skeleton. Once you recognize it, the pattern becomes automatic:

The Universal DMA Program Skeleton

#include <stdio.h>
#include <stdlib.h>

int main() {
    /* 1. Declare pointers (no size yet) */
    int *ptr = NULL;
    int n;

    /* 2. Ask the user for the size */
    printf("Enter number of elements: ");
    scanf("%d", &n);

    /* 3. Allocate memory at runtime */
    ptr = (int *) malloc(n * sizeof(int));
    if (ptr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    /* 4. Use the memory - read, write, compute */
    /* ... your logic here ... */

    /* 5. Always free what you allocated */
    free(ptr);
    ptr = NULL;

    return 0;
}
                                    

5 checkpoints for every assignment:

  1. NULL check after every allocation. malloc() can fail.
  2. Only allocate what you need. Use n * sizeof(type), not a hardcoded number.
  3. Set pointers to NULL after free(). Prevents accidental use-after-free.
  4. Free every block exactly once. No leaks, no double frees.
  5. Match every malloc with a free. If a function allocates and returns a pointer, its caller must free it.

🎯 Key Takeaway: DMA problems are not hard because of the logic — they're hard because of lifecycle discipline. Allocate → check → use → free. Follow that order every time and you'll never leak memory.

Frequently Asked Questions About DMA

1. What is the difference between malloc and calloc?

malloc(n * size) allocates a single block of n * size bytes and leaves the content uninitialized. calloc(n, size) allocates n blocks of size bytes and zero-initializes every byte.

2. Why must I check if malloc returns NULL?

If the system is out of memory, malloc returns NULL. Using that pointer without checking causes undefined behavior — often a crash. Always check and handle the failure gracefully.

3. What is a memory leak?

A memory leak happens when you allocate memory with malloc and forget to free it. The program keeps running but the memory is never returned to the OS. In long-running programs, leaks eventually exhaust available memory.

4. Do I need to cast the result of malloc in C?

Not strictly — C allows implicit conversion from void * to any pointer type. However, casting is common and harmless, and it keeps your code compatible with C++ compilers if you ever port it.

💡 Tip: Run your solutions under valgrind or compile with -fsanitize=address to catch leaks and out-of-bounds writes instantly.

📖 Related Tutorials

  • malloc in C
  • calloc in C
  • realloc in C
  • free in C
  • DMA Examples in C
  • DMA Interview FAQ

Previous Topic: -->> Storage Class Assignments in C   ||   Next topic: -->> Case Studies in C


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.