Indicate the class, implementation, main, and show output results please and tha
ID: 3725812 • Letter: I
Question
Indicate the class, implementation, main, and show output results please and thank you.
Program 1 - Create a class called TEST.
Write the following 4 functions for it.
Function 1
void testNullPtr().
In it, Declare a pointer int * ptrInt.
Assign NULL to the pointer.
What happen when you dereference a NULL pointer ?
( *ptrInt = 42; )
Function 2
void memoryLeak().
In it, Declare a pointer double * ptrDouble
Assign a new double to ptrDouble.
DeReference it and assign 3.123456789 as the value
Function 3
Declare the function header: string * deletePointer()
In it, Declare a pointer string * ptrString
Assign a new string to ptrString.
DeReference it and assign "Carlos" as the value.
write a return statement - for the pointer ptrString
Function 4
void testTwoAlias()
In it, Declare a pointer int * ptrAlias1
Assign new int to ptrAlias1
De-reference it and assign a value of 42
Also, Declare a 2nd pointer int * ptrAlias2 in the function.
copy the ADDRESS in ptrAlias1 to ptrAlias2
cout the address values stored in ptrAlias1 and ptrAlias2
cout the dereferenced values of ptrAlias1 and ptrAlias2
Explain the results
In the MAIN() function
Declare and instance of the Test class. TC1.
Test 1 - call the testNullPtr function, what happens.. explain it clearly.
Test 2 - Write a loop that runs a billion times. In the loop call memoryLeak() function.
Explain your results.
Test 3 - In the main function, declare a string *ptrS
Call the deletePointer function, assign deletePointer return value to ptrS.
Dereference ptrS. What do you get ?
Explain your results.
Test 4 - Call the testTwoAlias function.
Explain the results
Explanation / Answer
I have explained everything in the comments before the tests.
Note for test2 that to run a loop for billion time it would take a lot of seconds, so it would be become difficult to distinguish whether the program is running or the system has crashed. So, I have created an array of 1000 doubles every time the function is called. It would make the system crash/lag fast.
If you like the answer, don't forget upvote it :D
Feel free to ask about any queries/doubts, I would be happy to help!
Code
//template.cpp
#include <bits/stdc++.h>
using namespace std;
class TEST{
public:
void testNullPtr(){
int *ptrInt = NULL;
*ptrInt = 42;
}
void memoryLeak(){
double *ptrDouble = new double[1000];
ptrDouble[0] = 3.123456789;
}
string *deletePointer(){
string *ptrString = new string("Hello");
cout << "Address of string : " << ptrString << endl;
*ptrString = "Carlos";
return ptrString;
}
void testTwoAlias(){
int *ptrAlias1 = new int;
*ptrAlias1 = 42;
int *ptrAlias2 = ptrAlias1;
cout << "Address: ";
cout << ptrAlias1 << " " << ptrAlias2 << endl;
cout << "Values: ";
cout << *ptrAlias1 << " " << *ptrAlias2 << endl;
}
};
int main(){
TEST TC1;
//TEST1
//We try to dereference a memory location, that doesn't belong
//to the program (address 0 or NULL isnt given to any program, it is
//reserved for the system)
//thus the operating system aborts the program, sending a segmentation fault
cout << "Test1 ";
TC1.testNullPtr();
//TEST2
//Note that this would cause, a system crash if left
//This happens because once we need more memory than allocated, the OS tries
//to give memory through virtual memory(degrades performance though).
//Still if thats not enough it consumes more space...
//Soon it would consume whole of the memory.
//RISK : this can crash the system or make it very very slow.
//You might need to restart to go try the remaining tests
cout << "Test2 ";
for(int i = 0; i < 1000000; i++){
cout << i << endl;
TC1.memoryLeak();
}
//TEST3
//The string dynammically created, has a address,
//The value carlos is assigned at that value.
//so this is a correct code, the return value should be stored
//and should be used to free the allocated space
cout << "Test3 ";
string *ptrS = TC1.deletePointer();
cout << ptrS << " " << *ptrS << endl;
//TEST4
//They have the same address
//so the last update is shown only
cout << "Test4 ";
TC1.testTwoAlias();
return 0;
}