05 Jun GSP 215 Week 5 Homework memory Leaks NEW
Week 5 Homework—Memory Leaks
Memory leaks are bugs in C++ applications that can cause performance problems with your application or even causing it to crash. A memory leak is the result of failing to deallocate memory that was previously allocated. In C++ the commands
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
will enable the debug heap functions. After enabling the debug heap functions, place a call to _crtDumpMemoryLeaks() before an application exit point.
Given the following code, run this code in debug mode. The memory leak report will appear in the Output Window with the debug option. It should look something like this.
The output will look like the following.
Detected memory leaks!
Dumping objects ->
{142} normal block at 0x0079A948, 25 bytes long.
Data: <> CD CDCDCDCDCDCDCDCDCDCDCDCDCDCDCD
{141} normal block at 0x0079A8F8, 20 bytes long.
Data: <Sheldon > 53 68 65 6C 64 6F 6E 00 CD CDCDCDCDCDCDCD
Object dump complete.
The information displayed is: the memory allocation number (142), block type (normal), the hexadecimal memory location (0x0079A948), and the size of the block (25 bytes long).
Rewrite the code to remove the memory leaks, and submit the completed code with a screenshot of the output window with no memory leaks detected.
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <string>
void memLeak()
{
int *p = new int;
char * string1 = new char[20];
char * string2 = new char[25];
strcpy(string1, “Sheldon”);
string2=string1;
delete p;
}
int main(intargc, char* argv[])
{
memLeak();
_CrtDumpMemoryLeaks();
return 0;
}
C++ code with no memory leaks:
Screenshot:
Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.
About Writedemy
We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.
How It Works
To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Are there Discounts?
All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.
