Dynamic Memory Allocation
Dynamic Memory Allocation
Statistics of Rainfall
The weather station of each city has the detail
of rainfall in a year. Given the date and cm of
rainfall recorded on that day, write a C
program to determine the rainfall recorded in
each month of the year and average monthly
rainfall in the year.
Rainfall problem
FFF0
FFF1
ptr FFF0
FFF1
Output:
Dereferencing Example
#include <iostream.h>
ptr FFF0 FFF4
void main()
{ FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
cout << ptr << *ptr << endl; data FFF4 50.8
*ptr = 27.4; FFF5
cout << *ptr << endl;
cout << data << endl; FFF6
}
Output:
FFF4 50.80
Dereferencing Example
#include <iostream.h>
ptr FFF0 FFF4
void main()
{ FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
cout << ptr << *ptr << endl; data FFF4 27.4
*ptr = 27.4; FFF5
cout << *ptr << endl;
cout << data << endl; FFF6
}
Output:
Dereferencing Example
#include <iostream.h>
ptr FFF0 FFF4
void main()
{ FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
cout << ptr << *ptr << endl; data FFF4 27.4
*ptr = 27.4; FFF5
cout << *ptr << endl;
cout << data << endl; FFF6
}
Output:
27.4
Dereferencing Example (Cont ..)
#include <iostream.h>
ptr FFF0 FFF4
void main()
{ FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
cout << ptr << *ptr << endl; data FFF4 27.4
*ptr = 27.4; FFF5
cout << *ptr << endl;
cout << data << endl; FFF6
}
Output:
27.4
Operations on Pointer Variables
• Assignment – the value of one pointer variable can
be assigned to another pointer variable of the same
type
• Relational operations - two pointer variables of the
same type can be compared for equality, and so on
• Some limited arithmetic operations
– integer values can be added to and subtracted
from a pointer variable
– value of one pointer variable can be subtracted
from another pointer variable
Pointers to arrays
• A pointer variable can be used to access the elements
of an array of the same type.
int gradeList[8] = {92,85,75,88,79,54,34,96};
int *myGrades = gradeList;
cout << gradeList[1];
cout << *myGrades;
cout << *(myGrades + 2);
cout << myGrades[3];
• Note that the array name gradeList acts like the
pointer variable myGrades.
Arithmetic Operators and Pointers
• Four arithmetic operators can be used on
pointers: ++, --, +, and –
• Consider that ptr is an integer pointer which
points to the address 1000
• Assume 4 bytes are allocated for integers
• After the following operation
ptr++
ptr will point to location 1004
• On integer pointers -- will decrement by 4
Arithmetic Operators and Pointers
• Adding 1 will also be done based on the type of
pointer
• If it is character pointer then 1 is added to the
pointer variable
• If the pointer is of type double then 8 is added to the
pointer variable
• Subtraction is also completely based on the type of
pointer variable
Arrays and Pointers
• An array name in C contains the address of the
byte of memory allocated
• But array name is a “Pointer Constant”
Example:
int num = 45 , *ptr , **ptr2ptr ;
ptr = #
ptr2ptr = &ptr;
Dynamic Memory Allocation
malloc()
• Dynamically allocates memory when required
Syntax
• void * calloc (size_t n, size_t size);