Вы находитесь на странице: 1из 5

9/28/2018

Why Dynamic Memory Allocation is needed?


Computer Engineering

Computer Engineering
• If the specific size of memory required to store values is not
CO1302 Programming for Engineers known at the beginning of the programme.
• Therefore, the memory can be allocated during the programme
Department of Computer Engineering as required.
University of Sri Jayewardenepura

by Dr. Randima Dinalankara, randima@sjp.ac.lk

1 2

Before dynamic allocate memory... DMA


Computer Engineering

Computer Engineering

What happen in this code

//declare variables
int value = 10;
int *pntValue = NULL;

//assign value to a pointer


pntValue = &value;

3 4

Use a variable dynamically Use a variable dynamically


Computer Engineering

Computer Engineering

Example Example
//declare a pointer
//declare a pointer int *pntValue = NULL;
int *pntValue = NULL; //dynamically allocate memory
pntValue = new int;
//dynamically allocate memory ...
pntValue = new int; //delete the dynamically allocate memory
delete pntValue;

5 6

1
9/28/2018

Example Dynamically allocating an array


Computer Engineering

Computer Engineering
Example
int *pntValue = NULL; //declare a pointer
pntValue = new int; //DMA
*pntValue = 10; //store 10 in the HEAP
const int size = 10; //declare array size
int *pntValues = NULL; //declare pointer
cout<< ”pntValue \t= “ <<pntValue<<endl; //value pntValues = new int[size]; //Array in HEAP
cout<< ”*pntValue \t= “ <<*pntValue<<endl; //content in heap
cout<< ”&pntValue \t= “ <<&pntValue<<endl; //address

delete [] pntValues; //free the HEAP memory


delete pntValue; //delete location of heap

7 8

Question Example
Computer Engineering

Computer Engineering

• What is the advantage of using DMA for allocating space for an There are five houses in a road call '5th lane'. A survey was done
array? to collect the ages of the people in each of the house. The details
are given below.
• a pointer can be used to access arrays with different sizes or House 1: 29 and 27 yrs
array size can be changed during the programme. House 2: 7, 12, 35, and 34 yrs
House 3: 27, 50, and 54 yrs
House 4: 1, 4, 8, 34, 37, 68, 71, 62, and 65 yrs
House 5: 72 yrs

9 10

DMA for an array using mallac() & free() DMA for an array using mallac() & free()
Computer Engineering

Computer Engineering

• Quite old way, used in C programming • Quite old way, used in C programming
syntax
pointer indicator
syntax
pointer = (data_type *) malloc(no_of_element * size_of_data_type);
pointer = (data_type *) malloc(no_of_element * size_of_data_type);
free(pointer);
free(pointer); multiplication

11 12

2
9/28/2018

Example
Computer Engineering

Computer Engineering
const int count = 10; //declare array size
int *pntValues = NULL; //declare pointer

pntValues = (int *) malloc(count * sizeof(int) );


... Structures
A collection of variable(s) and array(s)
free(pntValues); //free the HEAP memory

13 14

Define a structure Define a structure


Computer Engineering

Computer Engineering

Example 1 Example 2

struct Book struct Student


{ {
char name[100];
int noOfPages;
int age;
float price;
bool gender;
};
char indexNo[10];
};

15 16

Declare a structure variable Access the elements of structure


Computer Engineering

Computer Engineering

Syntax: struct Book Use dot ( .) operator


<structure name> <variable name>; {
int noOfPages; Eg:
Eg: float price; myBook .noOfPages = 65;
//define a structure called Book };
myBook .price = 45.50;
struct Book //declare structure variable
{ Book madolduwa; //create a Book variable
cout<<myBook.price;
int noOfPages; Book oliverTwist; Book myBook;
float price; cout<<myBook.noOfPages;
};
17 18

3
9/28/2018

Exercise 1 Exercise 1
Computer Engineering

Computer Engineering
• Develop a structure to store • Develop a structure to store the date details.
the date details.
struct Date
(consider that the name of the {
month is needed to be stored as int year;
well)
unsigned int month, day;
char nameOfTheMonth[20];
};

19 20

Exercise 2 Exercise 3
Computer Engineering

Computer Engineering

• Create a structure to store properties of a rented room such as • Develop a structure to keep record of features of a car
follows • Manufacturer
• area size • Model
• direct access to a wash room • color
• number of windows • fuel type
• restrictions on electricity usage • number of doors
• rental fee • new/used
• etc. • millage
• ground clearance
• etc.

21 22

Exercise 4 Array of structures


Computer Engineering

Computer Engineering

A cannon fire a cannon ball with certain speed. Develop a


structure to store the properties of the cannon ball.
• First, define the structure
struct CannonBall
{
float weight, radius;
• Secondly, declare an array
float speed, angle; • structure is the data type
char madeOfMaterial[100];
};

23 24

4
9/28/2018

Array of structure elements Array of structure elements (cont.)


Computer Engineering

Computer Engineering
Eg: Eg:
struct Point for (int a = 0 ; a<3 ; a++)
{ {
int x; //x coordinate of the point cout<<”point no. “<< a<<endl;
int y; //y coordinate of the point cout << “x = “;
}; cin>>trianglePoints[a].x ;
cout << “y = “;
Point trianglePoints[3]; //array with 3 points cin>>trianglePoints[a].y ;
}

25 26

Exercise Structure inside another structure


Computer Engineering

Computer Engineering

struct Point struct Circle


• Develop a programme to store Cartesian coordinates of a { {
triangular points in a 2D plane via a structure and calculate the int x; Point Center;
total length of the triangle's edge.
int y; int radius;
}; };

27 28

Access structure inside another structure Exercise


struct Point
Computer Engineering

Computer Engineering

{
There are eight planets in the solar system. There position can be
int x; given a 3D space with respect to the sun. The speed, orbit
Circle myCircle;
int y; distance, composition, and etc. details are available for each of
}; them.
myCircle.Center.x = 30;
myCircle.Center.y = 45; struct Circle Develop structure(s) to store planet details.
{
Point Center;
int radius;
}; 29 30

Вам также может понравиться