0% found this document useful (0 votes)
3 views

Week 6 Pointers and Dynamic Memory Allocation Lecture

The document covers pointers and dynamic memory allocation in C programming, including pointer arithmetic and the use of functions like malloc, calloc, realloc, and free. It outlines learning objectives, common mistakes, and includes activities and quiz questions to reinforce understanding. The primary textbook reference is 'C Programming: A Modern Approach' by K. N. King.

Uploaded by

joekingehigie
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Week 6 Pointers and Dynamic Memory Allocation Lecture

The document covers pointers and dynamic memory allocation in C programming, including pointer arithmetic and the use of functions like malloc, calloc, realloc, and free. It outlines learning objectives, common mistakes, and includes activities and quiz questions to reinforce understanding. The primary textbook reference is 'C Programming: A Modern Approach' by K. N. King.

Uploaded by

joekingehigie
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Pointers and Dynamic Memory

Allocation
• Week 6 Lecture Notes
• Instructor: Isaac Muckson Sesay
• Contact Information:
[email protected]
Overview
• Introduction to pointers in C
• Pointer arithmetic and relationships with
arrays
• Dynamic memory allocation: malloc, calloc,
realloc, and free
Learning Objectives
• By the end of this week, students will:
• - Understand the concept and use of pointers.
• - Perform pointer arithmetic.
• - Use dynamic memory allocation effectively.
Introduction to Pointers
• - Pointers are variables that store memory
addresses.
• Syntax:
• ```c
• dataType *pointerName;
• ```
• Example:
• ```c
• int a = 10;
Pointer Arithmetic
• - Perform arithmetic on pointers to navigate
memory.
• Example:
• ```c
• int arr[3] = {1, 2, 3};
• int *p = arr;
• p++;
• printf("%d", *p); // Output: 2
• ```
Dynamic Memory Allocation
• - `malloc`: Allocates a block of memory.
• Syntax:
• ```c
• pointer = (dataType *)malloc(size);
• ```
• Example:
• ```c
• int *ptr = (int *)malloc(5 * sizeof(int));
• ```
Using calloc and realloc
• - `calloc`: Allocates multiple blocks and
initializes them to zero.
• Syntax:
• ```c
• pointer = (dataType *)calloc(num, size);
• ```
• Example:
• ```c
• int *ptr = (int *)calloc(5, sizeof(int));
Freeing Memory
• - Use `free` to release dynamically allocated
memory.
• Syntax:
• ```c
• free(pointer);
• ```
Common Mistakes
• - Forgetting to free memory, causing memory
leaks.
• - Dereferencing null or uninitialized pointers.
• - Accessing memory beyond allocated bounds.
Activity: Dynamic Array
• 1. Write a program to create a dynamic array
using `malloc`.
• 2. Populate the array with integers.
• 3. Resize the array using `realloc` and add
more elements.
• 4. Free the allocated memory.
Book Reference
• Primary Textbook:
• C Programming: A Modern Approach by K. N.
King
• - Chapter 9: Pointers and Dynamic Memory,
Pages 157-180
Quiz Questions
• 1. Define pointers and explain their use in C
programming.
• 2. Write a program to dynamically allocate
memory for a 2D array.
• 3. What is the difference between `malloc`
and `calloc`?
Conclusion
• Recap of Week 6 topics
• Next Week: Structures and File Handling
• Contact information for questions

You might also like