Unable to understand the breakpoint due to deletion of pointer array

Ok, so I am making a program in which I will be creating functions of different string operations.
I have made some and they are working fine as well. However, I am constantly facing a break point every time I run the program and it is not ending the program (even though the output is totally correct)
For example, here is the code (I have shared code of only that function which I will be using here as reference)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;
char *strcat_s(char *, const char *);
int strcmp(const char *, const char *);
char *strcpy_s(char *a, const char *b);
int main()
{
	int i = 0; 
	char a[100];
	cout << "Enter First String: ";
	cin.getline(a, 100);
	char b[100];
	cout << "Enter Second String: ";
	cin.getline(b, 100);
	char *z = new char[100];
	cout << "Perform the operation, " << endl
		<< "1) Strcat" << endl
		<< "2) strcmp" << endl
		<< "3) strcpy" << endl;
	int choice;
	cout << "Your selection: ";
	cin >> choice;
	switch (choice)
	{
	case 1:
		z = strcat_s(a, b);
		cout << endl << z << endl;
		break;
	case 2:
		cout << endl << strcmp(a, b) << endl;
		break;
	case 3:
		z = strcpy_s(a, b);
		while (z[i] != '\0')
		{
			cout << z[i];
			i++; 
		} 
		break;
	}
	delete[]z;
	cout << endl; 
	system("pause");
	return 0;
}
char *strcat_s(char *a, const char *b)
{
	char *p = &a[0];
	while (*p != '\0')
		p++;
	for (int i = 0; b[i] != '\0'; i++)
	{
		*p = b[i];
		p++;
	}
	return a;
}
char *strcpy_s(char *a, const char *b)
{
	int i = 0;
	char *p = a;
	while (b[i] != '\0')
	{
		*p = b[i];
		p++;
		i++;
	}
	a[i] = '\0';
	return a;
}


Ok, so here you see two string functions, strcpy and strcat...
Now, here is a sample output using 2nd function,
1
2
3
4
5
6
7
8
Enter First String: Hello
Enter Second String: cplusplus
Perform the operation,
1) Strcat
2) strcmp
3) strcpy
Your selection: 3
cplusplus


As you can see, it has done the job perfectly by copying "cplusplus" into first string but it doesn't show the system pause here..
However, when I remove that line of,
 
delete[]z; 

After doing this, the code executes perfectly. From what I know, there is no error in this deletion line and it should work without any problem. So, why is it still causing problem? Can someone explain, please?
Thank you!
Try only delete z without [].That is used when deleteing array of pointers
1
2
3
4
5
6
7
char ** pointer=new char*[10];
for(int i=0;i<10;i++)
pointer[i]=nullptr;//just for the example
//now deleting
for(int i=0;i<10;i++)
delete pointer [i];
delete [] pointer;// Now it has [] 

Edit:
The same can be done with dynamic 2d allocated array;
An example with pointer type int,but it can be anything
1
2
3
4
5
6
7
8
9
10
11
12
int ***Matrix=new int**[10];
for(int i=0;i<10;i++)
{
Matrix[i]=new int*[10];//allocating columns
for(int j=0;j<10;j++)
Matrix[i][j]=0;
}
//Now deleting it
for(int i=0;i<10;i++)
delete [] Matrix[i];//for every new that allocates an array you must have delete []
delete [] Matrix;

Last edited on
Thanks for the reply! ^_^

I just tried doing only "delete z" and it still was breaking my program at that point and didn't show me the system pause :/
I still don't get what's the problem here :(
You haven't posted your routine strcmp, so the code won't compile as-is.

Commenting out the call to that routine,

- change line 33 to either
z = strcpy_s(z, b);
or just
strcpy_s(z, b);

Then option 3 will work.

Option 1 requires you to re-factor your code. strcat doesn't do what you think it does.


Pointer z is used to access your dynamically-allocated, unnamed array of heap memory. You can't just change it to point elsewhere.
Topic archived. No new replies allowed.