Having issues with dynamically allocated arrays and memory leaks

Hey everyone.
I'm working on some past papers for an upcoming C++ exam.
Here's an exercise where you're supposed to fill in the blanks in order to avoid memory leaks and have everything run smoothly.
Could anyone help out? I know we're supposed to delete the dynamically allocated memory with delete [] array1 and delete [] array2 but I don't know what to do for array1 = array2. Can you even do that?

Thanks a lot!

1
2
3
4
5
6
7
  char * array1 = new char[50]; array1[1] = ‘c’;
_________________________________; ________________________________;
char * array2 = new char[60]; array2[1] = ‘d’;
_________________________________; ________________________________;
array1 = array2;
_________________________________; ________________________________;
if ( ____________________________ ) { cout << array1[1]; }
This makes no sense to me. This is more like a guessing game, there is no way to know what code is suppose to be on the blank spaces. are there any instructions with this? How are we supposed to know what's supposed to be in that if statement?

All I can think of is that, after array1 = array2; there is no reason to keep array2. So you can probably delete array2 after that.
Last edited on
You need to delete array1 before assigning a new value. Otherwise you have no way to access the memory previously allocated, resulting in a memory leak.

1
2
3
4
5
6
7
8
9
10
11
12
13
char * array1 = new char[50];  

char * array2 = new char[60]; 

delete [] array1; // need to do this before changing the value of array1 

array1 = array2;

// more code here.


// when finished using the array:
delete [] array2; 


All I can think of is that, after array1 = array2; there is no reason to keep array2. So you can probably delete array2 after that.

That wouldn't resolve the memory leak, It would simply mean that both array1 and array2 are now invalid pointers. (they point to memory which no longer belongs to the program).
Last edited on
@TarikNeaj We are supposed to fill the code in the blanks in order for the code to run without memory leaks. Does that make sense? We are not allowed to edit the written lines of code nor change the order of lines.
I guess at the if statement, we need to have some sort of "check" that the cout << array1[1] << endl; will print out fine.

Also: Even by appropriately using delete [] for both arrays to avoid memory leaks, I can't print the array1[1] at the last line with the if. I don't know what I should add in the "if" statement.
Last edited on
I mean. It doesnt really make sense. Look at @Chervil's code.

For example, the first 2 blank spaces, between the first and the second array

1
2
3
char * array1 = new char[50]; array1[1] = ‘c’;
_________________________________; ________________________________; // this one
char * array2 = new char[60]; array2[1] = ‘d’;


There doesn't need to be anything there. So It's hard to understand what should be there, more of a guessing game, you can put any code there. And same with the if statement, you can have
lots of conditions.

But if it's just about avoid memory leaks, then look at £Chervil's code
Yes, I understand. I guess you don't have to fill every single blank as long as you're sure that there are no memory leaks.
How can I have the line cout << array1[1]; though? If you just add the two delete [] staements, it still doesn't work. What would you write in the if, in order for that line to work ok?
Well, in my example, I tried to make sure that each new had a corresponding delete.

Possibly in the original question, the code is just a fragment of code, rather than a complete example. If the last delete [] isn't shown, that doesn't automatically imply a memory leak, as we have no way of knowing what the rest of the code will be.

Yes and that's the answer, that you have to make sure that every new has a corresponding delete.
I just can't find a way to print the array1[1] value at the last line. Don't know why.
Topic archived. No new replies allowed.