Corruption of Head

I am writing a code which includes a dynamic variable. I am getting an error about a corruption of head.

I knew that somehow I misused the dynamic variable but I can not see how.

Please find my code below and thank you for assistance.

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
 #include <iostream>
#include <cmath>

using namespace std;

int main () 
{	
	int m,n;
	int i,j;
	cout<<"please enter a the number of affine inequalities forming your polytope"<<endl;
	cin>>m;
	cout<<"please enter a the dimension of your space"<<endl;
	cin>>n;
	cout<<endl;

	/* Intialize Problem 1: mDim, nblock, trust  */
	int l,sum;
	l = ceil(log(static_cast<double>(n))/log(2.0));
	sum = 0;
	for (i=0;i<l;i++)
	{
		sum = sum + pow(2.0,i);
	}
	int mDim =  n*(n+2)+1+sum , nblock = m+2+sum ;
	cout <<"# primal var:  " << mDim <<endl;
	cout <<"~ blocks:  " << nblock << endl;

	int *blck_str;
	blck_str = new int(nblock);
	for(i=0;i<m;i++)
	{
		blck_str[i] = n+1;
	}
	blck_str[m] = 2*n;
	for(i=m+1;i<nblock-1;i++)
	{
		blck_str[i] = 2;
	}
	blck_str[nblock-1] = 1 ; 
	/*for(i=0;i<nblock;i++)
	{
		cout << blck_str[i] << "  "<<endl;
	}*/
	delete [] blck_str;
    system ("pause");
	return 0;
}
 
new int(nblock);

This creates allocates a single int with the value of nblock.

 
new int[nblock];

This would allocate an array of nblock integers.
Line #29 should declare a array of it's with square brackets:

 
blck_str = new int[nblock];


You might as well combine lines#28 and #29:

 
int* blck_str = new int[nblock];
Thank you again Peter87 and ajh32
Topic archived. No new replies allowed.