Computer Fundamentals with C and Unix
(Pointers and Dynamic Memory Allocation)
Lecture 10: Jan. 13, 2023 Prof. K.R. Chowdhary : Professor of CS
Disclaimer: These notes have not been subjected to the usual scrutiny reserved for formal publications. They may be distributed outside this class only with the permission of the Instructor.
The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.
Consider the following example to define a pointer which stores the address of an integer.
int n = 10; int *p = &n; printf("\%d", *p);
In above, p is variable, called pointer, because it has ”*” before it. This p is assigned the memory address of integer n by &n.
The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer.
int *a;//pointer to int char *c;//pointer to char
#include<stdio.h> int main(){ int n=50; int *p; p=&n; //stores the address of n variable printf("Address of p variable is %x \n",p); /* p contains the address of n therefore printing p gives the address of number.*/ printf("Value of data in p is %d \n",*p); return 0; }
Fig. 10.1 explains the working of pointer type of data, and what it references.
Uses of pointers:
To pass arguments by reference.
For accessing array elements.
To return multiple values.
Dynamic memory allocation.
To implement data structures.
Implementing a data structure
To do system-level programming where memory addresses are useful.
Memory allocations, in general, mean where computer programs and services are executed to reserve partially or complete space or virtual memory of a computer, this process is known as memory allocation. This process is hardware operation and is achieved by memory management through Operating systems and software applications. In general, there are static and dynamic memory allocations, whereas, in C programming language, we will see about dynamic memory allocation where programs are allocated during run time in memory and static memory allocation is a process of allocating memory while writing the C program which means memory is allocated at compile time.
In allocation, function memory is divided into program memory and data memory. Program memory consists of the memory used for main and functions. Data memory consists of permanent definitions, such as global data and constants, local declarations, and dynamic data memory.
How C handles these different needs is a function of the operating system and the compiler writer’s skills. Although the program code for a function may be in its memory at all times, the local variables for function are available only when it is active.
Furthermore, more than one version of the function can be active at a time. In this case, multiple copies of the local variables are allocated, although only one functional copy is present. The memory facility for these capabilities is known as stack memory. In addition to the stack, a memory allocation known as heap is available. Heap memory is unused memory allocated to the program and available to be assigned during its execution. It is the memory pool from which memory is allocated when requested by the memory allocation functions.
The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form.
malloc() allocates single block of requested memory.
calloc() allocates multiple block of requested memory.
realloc() reallocates the memory occupied by malloc() or calloc() functions.
free() frees the dynamically allocated memory.
The program instructions and global and static variables are stored in the region known as a permanent storage area.
Local variables are stored in the stack area.
The heap area is used for dynamic memory allocation auto run time.
The heap size keeps changing when the program is executed due to the creation and death of variables.
The Fig. 10.5 shows the memory allocation for program instructions, head, and stack.
The memory allocation in C is done using malloc() function in C. The C malloc() function stands for memory allocation. It is a function which is used to allocate a block of memory dynamically. It reserves memory space of specified size and returns the null pointer pointing to the memory location. The pointer returned is usually of type void. It means that we can assign C malloc() function to any pointer.
Syntax of malloc() Function:
ptr = (cast_type *) malloc (byte_size);
Here,
ptr is a pointer of cast_type.
The C malloc() function returns a pointer to the allocated memory of byte_size.
Example of malloc():
Example: ptr = malloc (50 * sizeof(int));
When this statement is successfully executed, a memory space of 50 bytes is reserved. The address of the first byte of reserved space is assigned to the pointer ptr of type int.
Summary:
We can dynamically manage memory by creating memory blocks as needed in the heap
In C Dynamic Memory Allocation, memory is allocated at a run time.
Dynamic memory allocation permits to manipulate strings and arrays whose size is flexible and can be changed anytime in your program.
It is required when you have no idea how much memory a particular structure is going to occupy.
Malloc() in C is a dynamic memory allocation function which stands for memory allocation that blocks of memory with the specific size initialized to a garbage value
Free() function is used to clear the dynamically allocated memory.
Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).
struct employee { int empid; char name[20]; float pay; }; struct employee emp[10];
struct address { char name[50]; char street[100]; char city[50]; char state[20]; int pin; };