0% found this document useful (0 votes)
26 views10 pages

Lab Manual-11

The document discusses pointers, arrays, and dynamic memory allocation in C. It introduces pointers and how they store memory addresses. It explains how to declare and initialize pointers and pointer arithmetic. It describes how to access arrays using pointers and pointer/offset notation. It discusses the importance of dynamic memory allocation and the malloc(), calloc(), realloc(), and free() functions for allocating and freeing memory dynamically at runtime.

Uploaded by

Hamza Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views10 pages

Lab Manual-11

The document discusses pointers, arrays, and dynamic memory allocation in C. It introduces pointers and how they store memory addresses. It explains how to declare and initialize pointers and pointer arithmetic. It describes how to access arrays using pointers and pointer/offset notation. It discusses the importance of dynamic memory allocation and the malloc(), calloc(), realloc(), and free() functions for allocating and freeing memory dynamically at runtime.

Uploaded by

Hamza Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CL1002 LAB 11

Programming Introduction to Pointers,


Accessing Arrays using Pointer,
Fundamentals Dynamic Memory Management

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES


Learning Objectives
1. Introduction to Pointers
2. Accessing Array using Pointers
3. Dynamic Memory

1.0 Introduction to Pointers


Pointer is a variable whose value is a memory address. Normally, a variable directly contains a specific value.
A pointer contains the memory address of a variable that, in turn, contains a specific value. In this sense, a
variable name directly references a value, and a pointer indirectly references a value.

1.1 Pointer Declaration & Initialization

Syntax: type * variable;

Code: int *ptr = 0; // Pointer Declaration


int var= 10;
ptr = &var; // Pointer Initialization

The value of the pointer variable ptr is a memory address. A data item whose address is stored in this variable
must be of the specified type.

int *ptr = 0; or int *ptr = NULL;


int var = 10;

var ptr

var = 10 NULL ptr = 0


10
&var = 6024 &ptr = 6000

6024 6000

ptr = &var;

var ptr

var = 10 ptr = 6024


10
&var = 6024 6024 &ptr = 6000
*ptr
6024 *ptr=10 6000
Sample Code:

1.2 POINTER ARITHMETICS

• A limited set of arithmetic operations may be performed on pointers. A pointer may be incremented
(++) or decremented (--), an integer may be added to a pointer (+ or +=), an integer may be subtracted
from a pointer (- or -=) and one pointer may be subtracted from another.
• When an integer is added to or subtracted from a pointer, the pointer is incremented or
decremented by that integer times the size of the object to which the pointer refers.
• Two pointers to elements of the same array may be subtracted from one another to determine the
number of elements between them.

2.0 Accessing Array using Pointers


Arrays and pointers are intimately related in C and often may be used interchangeably.
• An array name can be thought of as a constant pointer.
• Pointers can be used to do any operation involving array subscripting.
• When a pointer points to the beginning of an array, adding an offset to the pointer indicates which
element of the array should be referenced, and the offset value is identical to the array subscript. This
is referred to as pointer/offset notation.
• An array name can be treated as a pointer and used in pointer arithmetic expressions that do not
attempt to modify the address of the pointer.
• Pointers can be subscripted exactly as arrays can. This is referred to as pointer/subscript notation.
Sample Code:
OUTPUT:
3.0 Dynamic Memory
The process of allocating memory during program execution is called dynamic memory allocation. The
ability for a program to obtain more memory space at execution time to hold new nodes, and to release
space no longer needed is known as dynamic memory management.

3.1 Importance of Dynamic memory


Many times, it is not known in advance how much memory will be needed to store particular information in
a defined variable and the size of required memory can be determined at run time. For example, we may
want to hold someone’s name, but we do not know how long their name is until they enter it. Or we may
want to read in a number of records from disk, but we don’t know in advance how many records there are.
Or we may be creating a game, with a variable number of monsters (that changes over time as some monsters
die and new ones are spawned) trying to kill the player.

C language offers 4 dynamic memory allocation functions. They are,

Function Syntax
malloc () malloc (number *sizeof(int));
calloc () calloc (number, sizeof(int));
realloc () realloc (pointer_name, number * sizeof(int));
free () free (pointer_name);

Malloc()

malloc function is used to allocate space in memory during the execution of the program.
malloc does not initialize the memory allocated during execution. It carries garbage value.
Malloc function returns null pointer if it couldn’t able to allocate requested amount of memory.

Calloc()

calloc function is also like malloc function. But calloc initializes the allocated memory to zero.
But, malloc doesn’t.

Realloc()

realloc function modifies the allocated memory size by malloc and calloc functions to new size.
If enough space doesn’t exist in memory of current block to extend, new block is allocated for
the full size of reallocation, then copies the existing data to new block and then frees the old
block.

Free()

free function frees the allocated memory by malloc, calloc, realloc functions and returns the
memory to the system.
Malloc & free Sample Code:
Calloc & free Sample Code:
Realloc Sample Code:
3.2 Difference between static memory allocation and dynamic memory allocation in C

Static memory allocation Dynamic memory allocation


In static memory allocation, memory is allocated In dynamic memory allocation, memory is
while writing the C program. Actually, user allocated while executing the program. That
requested memory will be allocated at compile means at run time.
time.
Memory size can’t be modified while Memory size can be modified while
execution. execution.
Example: array Example: Linked list

You might also like