Why my program breaks when I run it ?

Hello guys, I have a problem with my code. When I try to run it (I am using Visual studio 2015) the program brŠµaks.
Can anyone explain to me what am I doing wrong. Here is my code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main() {
	double s[2] = { 94, -205 };
	double g = -1.75;
	double *z[3] = { &g, new double[3], s + 1 };
	z[0][1] = -88;
	z[0][2] = 34;
	z[0][3] = 2.6;
	double *w = &g;
	double **h = new double*;
	*h = s;
	double **f = z;
	cout << s[0] << endl;
	*(*(f + 2) - 1) -= 8;
	cout << s[0] << endl;
           
        delete h;
        delete [] z;
	system("pause");
	return 0;
}
Last edited on
Try and learn how to debug, it's very useful.

It breaks because of this line - delete[] z;
Last edited on
I've tried it without delete [] z;
and it still won't run.
Runs fine without it - cpp.sh/4a52x (but you'll obviously have memory leak). I dont know how to proparly remove something like that, someone else will have to chime in on that.

Edit: Running it on visual studio does tell me that the variable s is corrupted, but only after it runs fine.
Last edited on
Ok I will try to fix it. Thank you for the help.
Since you only dynamically allocated one element of z you can only delete that one element. Something like: delete [] z[1];
Topic archived. No new replies allowed.