A hand? ;-)

I am supposed to do something to fix a memory leak?

// Week 2 Assignment-2
// Description: Problems with pointers and new/delete
//----------------------------------

//**begin #include files************
#include <iostream> // provides access to cin and cout

//--end of #include files-----------
//----------------------------------

using namespace std;
//----------------------------------

//**begin global constants**********
const int arraySize = 5;
//--end of global constants---------
//----------------------------------


//**begin main program**************
int main()
{
cout << endl << endl;
int* nArray = new int[arraySize];
cout << " --->After creating and allocating memory for nArray." << endl;
cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
for (int i = 0; i < arraySize; i++)
{
nArray[i] = i*i;
}
cout << " --->After initializing nArray." << endl;
cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl;
for (int i = 0; i < arraySize; i++)
{
cout << " nArray[" << i << "] = " << nArray[i] << " at address <" << nArray + i << ">" << endl;
}
// You'll need a command here to fix the memory leak
cout << endl << " --->Before reallocating memory for nArray." << endl;
cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << endl;
nArray = new int[arraySize + 2];
cout << dec << " --->After reallocating memory for nArray." << endl;
cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
for (int i = 0; i < arraySize + 2; i++)
{
nArray[i] = i*i;
}
cout << endl << " --->After reinitializing nArray." << endl;
cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl;
for (int i = 0; i < arraySize + 2; i++)
{
cout << " nArray[" << i << "] = " << nArray[i] << " at address <" << nArray + i << ">" << endl;
}
// . . . and also here.
cout << endl << " --->Getting ready to close down the program." << endl;
cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
// Wait for user input to close program when debugging.
cin.get();
return 0;
}
//--end of main program-------------
//----------------------------------
First of all, please use code tags and systematic indentation style.
See http://www.cplusplus.com/articles/jEywvCM9/
Well formatted code is much easier to read.
You can edit your post.

I am supposed to do something to fix a memory leak?

Yes. The comments in code state that rather clearly. They also hint where to do it.

The main thing is less about "fixing" anything, and more about understanding what is going on.
What help do you need? The comments in the program tell you exactly where you need to deal with memory leaks.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
whenever you use the keyword "new" it is always followed by a "delete". You need to delete your pointer once you have used it to fix the memory leak. If you don't call delete you will get memory leaks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// You'll need a command here to fix the memory leak
	cout << endl << " --->Before reallocating memory for nArray." << endl;
	cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << endl;
	delete[] nArray; //call it here
	nArray = new int[arraySize + 2];
	cout << dec << " --->After reallocating memory for nArray." << endl;
	cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
	for (int i = 0; i < arraySize + 2; i++)
	{
		nArray[i] = i*i;
	}
	cout << endl << " --->After reinitializing nArray." << endl;
	cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl << endl;
	for (int i = 0; i < arraySize + 2; i++)
	{
		cout << " nArray[" << i << "] = " << nArray[i] << " at address <" << nArray + i << ">" << endl;
	}
	// . . . and also here.
	
	cout << endl << " --->Getting ready to close down the program." << endl;
	cout << " nArray address is <" << nArray << "> and contains the value " << hex << *nArray << dec << endl;
	delete[] nArray; // and call it here 
Last edited on
Topic archived. No new replies allowed.