0% found this document useful (0 votes)
453 views

CSNB244 - Lab 6 - Dynamic Memory Allocation in C++

This document discusses dynamic memory allocation in C++. It covers using the new operator to dynamically allocate memory for objects and arrays at runtime. It provides examples of allocating memory for basic types using new, initializing allocated memory, allocating arrays of structures, and deleting allocated memory with delete. The document also provides a lab assignment to create a program to calculate purchases from a fruit shop using structures, pointers, and arrays.

Uploaded by

elijahjrs70
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)
453 views

CSNB244 - Lab 6 - Dynamic Memory Allocation in C++

This document discusses dynamic memory allocation in C++. It covers using the new operator to dynamically allocate memory for objects and arrays at runtime. It provides examples of allocating memory for basic types using new, initializing allocated memory, allocating arrays of structures, and deleting allocated memory with delete. The document also provides a lab assignment to create a program to calculate purchases from a fruit shop using structures, pointers, and arrays.

Uploaded by

elijahjrs70
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/ 5

Programming II with C++ (CSNB244) – Lab 6

Topics: Dynamic Memory Allocation in C++

In this lab session, you will learn on how to define the size of an array dynamically, allocate
/deallocate memory in a program, perform dynamic memory management.

new operator: dynamically allocate (i.e. reserve) the exact amount of memory required to
hold an object or array at execution time.
To return memories to the free store use the delete operator to deallocate (i.e. release) it.
int *ptr1 = new int;
...
delete ptr1;

Let’s see a real example.


#include <iostream>
#include <string>
using namespace std;

int main()
{
int x=3;
int *xptr1 = &x;
int *xptr2 = new int;
*xptr2 = 10;

cout << "*xptr1 " << *xptr1 << endl;


cout << "xptr1 " << xptr1 << endl;
cout << "&*xptr1 " << &*xptr1 << endl;
cout << "*&xptr1 " << *&xptr1 << endl;
cout < "----------------------" << endl;
cout << "*xptr2 " << *xptr2 << endl;
cout << "xptr2 " << xptr2 << endl;

delete xptr2;
return 0;
}

Write the output of the program.

Apply dynamic memory allocation for an object of a new type of structure.


Example:
struct fruit{
char name[20];
float price;
};

int main()
{
fruit *localPtr = new fruit;
fruit *internationalPtr = new fruit;
delete localPtr;
delete internationalPtr;
}
Page 1 of 5
COIT – NSMS Nov 2013
Programming II with C++ (CSNB244) – Lab 6
Let’s see a real example.
#include <iostream>
#include <string>
using namespace std;

struct fruit {
char name[20];
float price;
};

int main()
{
fruit *localPtr = new fruit;
fruit *internationalPtr = new fruit;

strcpy_s(localPtr->name, "Rambutan"); //assign value to a structure -


string
localPtr->price = 5.00; //assign value to a structure - float

cout << "International Fruit" << endl;


cout << "Enter a fruit name: ";
cin.getline(internationalPtr->name, 20);
cout << "Enter the price per kg: ";
cin >> internationalPtr->price;

cout << "I love to eat " << localPtr->name << endl;
cout << internationalPtr->name << " will cost me RM"
<< internationalPtr->price << " per kg." << endl;

delete localPtr;
delete internationalPtr;

return 0;
system("pause");

Write the output of the program.

Initializing Dynamic Memory - Pointer


An initializer can be provided for a newly created fundamental-type variable.

int *ptr1 = new int(101);


...
delete ptr1;

Page 2 of 5
COIT – NSMS Nov 2013
Programming II with C++ (CSNB244) – Lab 6
Full code example:
#include <iostream>
#include <string>
using namespace std;

int main()
{
int *ptr1 = new int(100);

cout << "*ptr1 " << *ptr1 << endl;


cout << "ptr1 " << ptr1 << endl;
cout << "&*ptr1 " << &*ptr1 << endl;
cout << "*&ptr1 " << *&ptr1 << endl;

delete ptr1;
return 0;
system("pause");
}

Write the output of the program.

Dynamic Allocating Arrays with new [ ]


Template codes for struct in C++.
#include <iostream>
using namespace std;

struct structName{
datatype variableInStruct1;
datatype variableInStruct3;
};

int main()
{
structName *arrayPtr = new structName[element]; //declare
variable. replace element with an integer

//assign value to an array of a structure


arrayPtr[element].variableInStruct1=value;

//printout value from pointerForStruct


cout << arrayPtr[element].variableInStruct1;

//destroy a dynamically allocated object. only for pointer


delete arrayPtr;
}
Page 3 of 5
COIT – NSMS Nov 2013
Programming II with C++ (CSNB244) – Lab 6

Full code example:


#include <iostream>
#include <string>
using namespace std;
#define SIZE 3

struct fruit {
char name[20];
float price;
};

int main()
{
fruit *localPtr = new fruit[SIZE];
int i;

strcpy_s(localPtr[0].name, "Rambutan"); //assign value to a structure -


string
localPtr[0].price = 5.00; //assign value to a structure - float

strcpy_s(localPtr[1].name, "Durian"); //assign value to a structure -


string
localPtr[1].price = 10.00; //assign value to a structure - float

strcpy_s(localPtr[2].name, "Mango"); //assign value to a structure -


string
localPtr[2].price = 18.00; //assign value to a structure - float

cout << "Welcome to Fruit Market" << endl


<< "Today's fresh fruit" << endl;
for (i = 0; i<SIZE; i++)
cout << "- " << localPtr[i].name << " RM"
<< localPtr[i].price << endl;

delete localPtr;

return 0;
system("pause");

Write the output of the program.

Page 4 of 5
COIT – NSMS Nov 2013
Programming II with C++ (CSNB244) – Lab 6
Lab Assignment (Carry Marks 5%)
There is a fruit shop name “Only Fresh Fruit Shop”. This shop sells the following fruits
and grouped by 2 different groups;

Fruit Name Price per kg (RM)


Sunkist Orange 2.00
Strawberry 22.00
Papaya 5.00
Star fruit 6.00
Kiwi 10.00

This shop has requested you to provide a program to calculate and provide the details
(receipt) of the purchase. You are required to use structure, pointer, and array to develop
this program. You may refer to the following figure to get as a sample of output. You may
also work in a group of 2 students per group.

You are required to show and explain this program to you lecturer on the next lab session.

Page 5 of 5
COIT – NSMS Nov 2013

You might also like