0% found this document useful (0 votes)
4 views2 pages

Tutorial 23

This C++ tutorial demonstrates the use of pointers and dynamic memory allocation. It initializes variables, arrays, and dynamically allocated memory, then outputs their addresses and values. Finally, it properly deletes the dynamically allocated memory to prevent memory leaks.

Uploaded by

kdbalke
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)
4 views2 pages

Tutorial 23

This C++ tutorial demonstrates the use of pointers and dynamic memory allocation. It initializes variables, arrays, and dynamically allocated memory, then outputs their addresses and values. Finally, it properly deletes the dynamically allocated memory to prevent memory leaks.

Uploaded by

kdbalke
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/ 2

Tutorial 23

#include <iostream>
using namespace std;

int main() {
double a = 10.7;

double* pa = &a;

double b[4] = { 1.1, 2.2, 3.3, 4.4 };

double* pb = b;

double* pc = new double;


*pc = 20.5;

double* pd = new double[4];


pd[0] = 5.3;
pd[1] = 6.9;
pd[2] = 7.8;
pd[3] = 8.9;

cout << "Memory Map:" << endl;

cout << "Name: a, Address: " << &a << ", Value: " << a << endl;

cout << "Name: pa, Address: " << &pa << ", Points to: " << pa << ", Value: " <<
*pa << endl;

for (int i = 0; i < 4; i++) {


cout << "Name: b[" << i << "], Address: " << &b[i] << ", Value: " << b[i] <<
endl;
}

cout << "Name: pb, Address: " << &pb << ", Points to: " << pb << ", Value: " <<
*pb << endl;

cout << "Name: pc, Address: " << &pc << ", Points to: " << pc << ", Value: " <<
*pc << endl;

for (int i = 0; i < 4; i++) {


cout << "Name: pd[" << i << "], Address: " << &pd[i] << ", Value: " << pd[i]
<< endl;
}

delete pc;
delete[] pd;

return 0;
}

You might also like