Please I need help with this assignment Lanuage C Beginner l

Please.. I need help with this assignment.

Lanuage: C++ (Beginner level)

If possible, please write it with comment (so that I can learn) and in Xcode.

***********************************************************************************************************************************************

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

Solution

The C++ program for the above requirement in as :-

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

class Test {

public:

void testNullPtr(){
int *ptrInt = NULL; // Deferencing the pointer
*ptrInt = 42;
}

void MemoryLeak(){
double *ptrDouble = 4.02345;
*ptrDouble = 3.123456789;
}

string *deletePointer(){
string *ptrString = \"Baba\";
*ptrString = \"Carlos\";     //Dereferencing the string pointer.

return ptrString;
}

void testTwoAlias(){
int *ptrAlias1 = 20; //Assigning the value
int *ptrAlias2;
ptrAlias2 = &(*ptrAlias1); //Copying the address of previous pointer
cout<<ptrAlias1;
cout<<ptrAlias2;
cout<<*ptrAlias1;
cout<<*ptrAlias2;
}
};
int main()
{
string *ptrs;
Test test;
test.testNullPtr();
test.MemoryLeak();
*ptrs = test.deletePointer();
*ptrs = \"New\";
cout<<*ptrs;
test.testTwoAlias();
return 0;
}

By dereferencing the null pointer, there is no specific behaviour that could happen.It can let you dereference or
it can result in the crash of the program. It totally depends on the architecture of OS.
While I try to execute the program from my end, it throws the dump.

Please.. I need help with this assignment. Lanuage: C++ (Beginner level) If possible, please write it with comment (so that I can learn) and in Xcode. *********
Please.. I need help with this assignment. Lanuage: C++ (Beginner level) If possible, please write it with comment (so that I can learn) and in Xcode. *********
Please.. I need help with this assignment. Lanuage: C++ (Beginner level) If possible, please write it with comment (so that I can learn) and in Xcode. *********

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site