Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write 3 different functions in C++ to create memory on the heap without causing

ID: 3797801 • Letter: W

Question

Write 3 different functions in C++ to create memory on the heap without causing a memory leak. You will need to assign the address to a pointer that was created outside the function. Remember, you can return an address or change what a pointer points to (the contents of a pointer) in a function by passing the pointer by reference or by passing the address of the pointer. What if you want to change the contents of what the pointer points to? Make a function that will set the contents of the space on the heap. Do you still need to pass by reference or the address of the pointer? Why or why not? How will you delete the memory off the heap? Try doing it outside a function, now inside a function. Make sure you delete function is setting your pointer back to NULL, since it is not supposed to be pointing anywhere anymore. You can check to see if you have any memory leaks using Val grind. % Val grind program_exe

Explanation / Answer

#include <iostream.h>
using namespace std;

/** Function to assign the content to the pointer
*
* @param pointer type variable ptr_var
*
* @return void
*/
void pointer_assign_content(int *ptr_var){
*ptr_var = 10;
}// end pointer_assign_content()

/** Function to update the content to the pointer
*
* @param pointer type variable ptr_var
*
* @return void
*/
void pointer_update_content(int *ptr_var){
   *ptr_var *= 10;
}// end pointer_update_content()

/** Function to delete the content to the pointer
*
* @param pointer type variable ptr_var
*
* @return void
*/
void delete_pointer(int *ptr_var){
   *ptr_var   =   NULL;
}// end delete_pointer()

int main() {
   // your code goes here
   int main_var = 0;
   // Assigning the content to the variable
   pointer_assign_content(&main_var);
   cout << "New value of variable main_var = " << main_var << endl;
   // Updating the content of the variable
   pointer_update_content(&main_var);
   cout << "New and updated value of variable main_var = " << main_var << endl;
   cout << "Deleting the contents of variable main_var" << endl;
   // Deleting the content of the variable
   delete_pointer(&main_var);
   cout << "New and updated value of variable main_var after its deletion = " << main_var << endl;
   return 0;
}// end main()

In second case, while changing the contents what the pointer points to, we still need to pass the parameter by reference because if we pass the parameter by value, then it will only pass the copy of that paramter in the function, which will not change the value that the pointer points to. Instead, we will pass the address of the parameter which will actually change the content that the pointer points to. Therefore, we still need to pass the parameter by reference.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote