Question

In: Computer Science

4.2 Lab: new and delete new is the operator used to dynamically allocate memory while the...

4.2 Lab: new and delete

new is the operator used to dynamically allocate memory while the program is running

delete is the operator used to release memory when it is no longer needed, so it could be used next time new is used

This lab gives you an example of using new and delete with an array of structures.

Your tasks are listed below:

  • review the existing code
  • finish writing the copyList()function

// Lab: new and delete
#include <iostream>
#include <string>
using namespace std;

struct Stu{
string name;
double gpa;
};

Stu *copyList(Stu list[], int n);
void printList(Stu *list, int n, string desc);

int main() {

Stu list[10] = {{"Tom", 3.5}, {"Bob", 2.9}, {"Ann", 3.2}, {"Dan", 3.1}, {"Zoe", 2.9}};
Stu *backup;
int n = 5;

printList(list, n, "Original");
backup = copyList(list, 5);
printList(list, n, "Backup");

// Release memory
delete [] backup;

return 0;
}
// This function dynamically allocates an array of n STU structures,
// copies data from list to the new array, one element at a time,
// and returns a pointer to the new array
Stu *copyList(Stu list[], int n)
{
Stu *backup;

/* write your code here */

return backup;
}

// This functions displays an array of structures
// Note: it doesn't matter if the array is
// statically allocated or dynamically allocated
void printList(Stu *anyList, int n, string desc)
{
cout << endl << desc << endl;
for (int i = 0; i < n; i++)
{
cout << anyList[i].name << " " << anyList[i].gpa << endl;
}
}

// please do not use any recursive functions or anything, this simply testing pointers

Solutions

Expert Solution

#include <iostream>
#include <string>
using namespace std;
struct Stu{
string name;
double gpa;
};
Stu *copyList(Stu list[], int n);
void printList(Stu *list, int n, string desc);
int main() {
Stu list[10] = {{"Tom", 3.5}, {"Bob", 2.9}, {"Ann", 3.2}, {"Dan", 3.1}, {"Zoe", 2.9}};
Stu *backup;
int n = 5;

printList(list, n, "Original");
backup = copyList(list, 5);
printList(backup, n, "Backup");

// Release memory
delete [] backup;

return 0;
}
// This function dynamically allocates an array of n STU structures,
// copies data from list to the new array, one element at a time,
// and returns a pointer to the new array
Stu *copyList(Stu list[], int n)
{
Stu *backup;
backup=new Stu[n];//allocate memory using new operator
for(int i=0;i<n;++i)
{
backup[i].name=list[i].name;
backup[i].gpa=list[i].gpa;
}


return backup;
}
// This functions displays an array of structures
// Note: it doesn't matter if the array is
// statically allocated or dynamically allocated
void printList(Stu *anyList, int n, string desc)
{
cout << endl << desc << endl;
for (int i = 0; i < n; i++)
{
cout << anyList[i].name << " " << anyList[i].gpa << endl;
}
}

Output


Related Solutions

Write a modified version of the program below. In this version, you will dynamically allocate memory...
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings).   Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory! #include <iostream>...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory so you store that number of integers. Write integers in order starting from 1 until you fill all that memory. Print the addresses and values of the first and the last integer stored in the memory.
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory so you store that number of integers. Write integers in order starting from 1 until you fill all that memory. Print the addresses and values of the first and the last integer stored in the memory. Help ASAP!!!
Constructors/Destructors - Initialize your data. Allocate memory if using a dynamically allocated array. The destructor should...
Constructors/Destructors - Initialize your data. Allocate memory if using a dynamically allocated array. The destructor should deallocate the memory. The constructor should take a single int variable to determine the size. If no size is specified (default constructor), then the size should be set to 50. operator[](size_t) - This should return the location with the matching index. For example if given an index of 3, you should return the location at index 3 in the list. Location class/struct - This...
Solve the question using 2 dimensional pointers. You must use new and delete operator in the...
Solve the question using 2 dimensional pointers. You must use new and delete operator in the program and try to make an easier logic of the problem. Q2. Write a C++ program using a dynamic array (or arrays) to assign passengers seats in a Bus and your program will ask the user how many rows the Bus has and will handle that many rows (Assume the Bus does not always have the same rows). (5 points) Expected output: Assume a...
While performing Lab 11, Electrochemistry, students connected 1 M solutions of the following metals and used...
While performing Lab 11, Electrochemistry, students connected 1 M solutions of the following metals and used electrodes to monitor the cell potential. Using the table of theoretical values and what you know of the metals to fill in the blanks below. When writing equations: Use spaces only before and after a plus sign. For example, the reaction: Fe2++2e−→Fe would be entered as: Fe^2+ + 2e^- →→ Fe Galvanic Cell Consisting of: Cu2+2e− →→ Cu 0.34 Al3++3e− →→ Al -1.66 E0cell...
Post-lab Question #1: While both NaBH4 and LiAlH4 can both be used toreduce ketones and aldehydes,...
Post-lab Question #1: While both NaBH4 and LiAlH4 can both be used toreduce ketones and aldehydes, the procedure that you followed in lab wouldnot result in the fluorenol product if NaBH4 were simply replaced with LiAlH4. a) Explain why. b) What change to the procedure would have to be made to use LiAlH4 tosuccessfully reduce fluorenone to fluorenol? Post-lab Question #2: NaBH4 is not capable of reducing esters since estersare less reactive than ketones. Explain structurally why this is the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT