Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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: 53 68 65 6C 64 6F 6E 00 CD CD CD CD CD CD CD CD

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions