Question
PART A: One of the dangers with C++ pointers is memory leaks. Run the following code in C++. #define _CRTDBG_MAP_ALLOC #define _CRT_SECURE_NO_WARNINGS #include #include #include
PART A:
One of the dangers with C++ pointers is memory leaks. Run the following code in C++.
#define _CRTDBG_MAP_ALLOC
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
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(int argc, char* argv[])
{
memLeak();
_CrtDumpMemoryLeaks();
return 0;
}
When you run this code you should see the following showing the memory leaks: Detected memory leaks!
Dumping objects ->
{72} normal block at 0x02BA6250, 25 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
{71} normal block at 0x02BA97F8, 20 bytes long.
Data:
How would you fix it so that the value in string1 and string2 are both Sheldon but with no memory leaks?
C++ code:
Screenshot of output:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started