Академический Документы
Профессиональный Документы
Культура Документы
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
1 2
Computer Engineering
//declare variables
int value = 10;
int *pntValue = NULL;
3 4
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
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
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
13 14
Computer Engineering
Example 1 Example 2
15 16
Computer Engineering
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
Computer Engineering
23 24
4
9/28/2018
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
Computer Engineering
27 28
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