In: Computer Science
1) Static allocation
a. Create two integer variables x and y, initialize them with different values.
b. Use static memory allocation, declare px and py as address of x and y separately.
c. Print out x, y, px, py, &x, &y, *px, *py.
d. Let py = px, and *py = 100
e. Print out x, y, px, py, &x, &y, *px, *py.
g. Print out *px++, x, px
2) Dynamic allocation and memory leak
Repeat the same steps of (2), by dynamic allocation. Make sure you deallocate pointers to avoid memory leaks.
3)Swap values
a. Create three swap functions that implement pass-by-value, pass-by-reference, pass-by-pointer
b. In the main function, test each function if they are swapping the values or not.
Here, we will discuss all the 3 questions one by one:
Note: For each question, I have explained everything in the comments of the program. So, please refer to it for understanding and clarity.
1.
Code(With explanation):
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x = 10, y = 50; // Created two variables and declared it with different values
int *px = &x, *py = &y; // Declared pointer as address of x and y separately
//Print out x, y, px, py, &x, &y, *px, *py
cout << "x: " << x << endl;
cout << "y: " << y << endl;
cout << "px: " << px << endl;
cout << "py: " << py << endl;
cout << "&x: " << &x << endl;
cout << "&y: " << &y << endl;
cout << "*px: " << *px << endl;
cout << "*py: " << *py << endl;
// py = px, and *py = 100
py = px;
*py = 100;
// Print out x, y, px, py, &x, &y, *px, *py
cout << "\nx: " << x << endl;
cout << "y: " << y << endl;
cout << "px: " << px << endl;
cout << "py: " << py << endl;
cout << "&x: " << &x << endl;
cout << "&y: " << &y << endl;
cout << "*px: " << *px << endl;
cout << "*py: " << *py << endl;
// Print out *px++, x, px
cout << "\n*px++: " << *px++ << endl;
cout << "x: " << x << endl;
cout << "px: " << px << endl;
return 0;
}
Output/Code-run:
2.
Memory leaks occur when a new memory is allocated dynamically and
never deallocated.
Code(With explanation):
#include <iostream>
using namespace std;
// Declared a class for use
class Plant {
public:
Plant() {
cout << "Constructor!" <<endl;
}
~Plant() {
cout << "Destructor!" <<endl;
}
};
int main() {
// Dynamic memory allocation with memory leak
cout << "\With memory leak: " << endl;
Plant* myPlantArray = new Plant[4];
// Dynamic memory allocation without memory leak
cout << "\nWithout memory leak: " << endl;
Plant* myPlantArray2 = new Plant[4];
delete[] myPlantArray2;
return 0;
}
Output/Code-run:
3.
Code(With explanation):
#include<bits/stdc++.h>
using namespace std;
// Swap function - Pass by value
void swapPassByValue(int a, int b)
{
int temp;
temp = b;
b = a;
a = temp;
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
}
// Swap function - Pass by reference
void swapPassByReference(int &x, int &y) {
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */
return;
}
// Swap function - Pass by pointer
void swapPassByPointer(int* a, int* b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
// Declaring two variables which we want to swap
int a = 100;
int b = 200;
// Pass by value
cout << "Pass by value: \n" << endl;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
// After calling pass by value, the value of a = 100 and b = 200, for the next step and we have printed its in the function itself.
/* calling a function to swap the values using pass by value.*/
swapPassByValue(a, b);
// Pass by reference
cout << "Pass by reference: \n" << endl;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
/* calling a function to swap the values using pass by reference.*/
swapPassByReference(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
// Pass by Pointer
cout << "Pass by pointer: \n" << endl;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
/* calling a function to swap the values using pass by pointer.*/
swapPassByPointer(&a, &b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;
}
Output/Code-run:
Please let me know in the comments in case of any confusion. Also, please upvote if you like.