char array Memory deAllocation

Hello Guys i am bothered that my dynamic array is not de allocating memory and gives an error
If i comment the lines "delete []arr and delete []arr1" in the code then the code works fine else it gives an error
Kindly Help


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;
char *StrCat( char *s1, int n, char z )
{
	int count=0;
	for(int a=0;*(s1+a)!='\0' ;a++)
		count++;

	int num=0;
	for(int a=count;a<count+n;a++)
	{
		*(s1+a)=z;
	}

	return s1;
}

int main()
{
char *arr1=new char [50];
int num;

char *arr=new char [50+num];

cout<<"Enter the number of times you want to concatinate"<<endl;
cin>>num;
cin.ignore();

cout<<"Please enter line 1 "<<endl;
cin.getline(arr1,50);

arr=StrCat(arr1 , num,'v');

for(int a=0;*(arr+a)!='\0';a++)
{
	cout<<*(arr+a);
}
cout<<endl;

delete[]arr1;
delete[]arr;

return 0;
}
You're changing the value of arr to the value of arr1, so the deletes are trying to delete arr1 twice (and not deleting arr at all). Just don't assign to arr. There's no reason to. arr1 is directly modified.

BTW, this is silly syntax: *(s1+a) or *(arr+a)
You just mean s1[a] or arr[a]

EDIT: Your StrCat is weird, too. It looks like it might write past the end.
Last edited on
Thanks for the help
But can you tell what edits i need to do so that the memory is de allocated properly and the functioning of the program is not disturbed

For the synatx We were instructed to do the whole question in pointer notation
I told you. Don't assign the return value of StrCat to arr. Don't assign it to anything. Just ignore it as is usually done with the return value of strcat, for example.
Last edited on
To elaborate on what Dutch said:
your program, condensed:

1
2
3
4
5
6
7
8
9
10
char *arr1=new char [50]; //get memory for arr1
char *arr=new char [50+num];//get memory for arr
arr=StrCat(arr1 , num,'v'); //reassign arr.  the memory you got for arr is now lost (leaked).  
{ //inside strcat:
     s1 IS arr1
   return s1; //which is ARR1
   threfore arr = arr1 now!
}
delete[]arr1;  //delete the memory you got for arr1
delete[]arr;  //delete arr1 again -- this is a no-no.  


If you still do not get it, you should google the beavis and butthead candybar episode. The candy bar is memory, and your pointer is the money paid for it. The inability to pay the teacher for their sales is your leak.
Last edited on
Thanks for your reply
Now for what i understand is that arr1 is being passed to the function as copy not by alias and the copy will have its seperate address so basicaly i am updating the value in the copy ?? My concept maybe vague So kindly correct me if i am wrong and when i return the address from the function arr points towards the copy and not actualy arr1

Like arr = copy arr1
Last edited on
look again.
s1 is indeed a copy. inside that copy is the value of the address of arr1.
return s1 returns the value of the address of arr1.
arr = return value.
therefore arr is now the arr1.
this happens OUTSIDE the function.
arr=StrCat(arr1 , num,'v');
^^^ that is effectively the same as saying
arr = arr1; //do you see this?
which drops the value of arr (what it was before is gone) which is your leak.
and later
arr == arr1 so double delete of it is no good.

it has nothing to do with the function. all the function does is hide the problem.
change the function call to arr = arr1 and see if you see why THAT is a big mistake. Then try to see why the function is doing an identical thing to that.
Last edited on
Really leant alot
Thank You
Topic archived. No new replies allowed.