The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 1 Day Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
I need help with this week's homework. I have attached the word doc containing the assignment.
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 CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
{141} normal block at 0x0079A8F8, 20 bytes long.
 Data: <Sheldon        > 53 68 65 6C 64 6F 6E 00 CD CD CD CD CD CD CD CD
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 = newint;
      char * string1 = newchar[20];
      char * string2 = newchar[25];
      strcpy(string1, "Sheldon");
     Â
      string2=string1;
      delete p;
}
Â
int main(intargc, char* argv[])
{
   memLeak();
      _CrtDumpMemoryLeaks();
     Â
      return 0;   Â
}
Â
Â
C++ code with no memory leaks:
Â
Â
Screenshot:
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly