dynamic array problem

This code is a function inside my FIR filter class, not the full code.
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
void clock()
	{
		ifstream storedFreq("stored_f_sf.txt");
		storedFreq>>f1;
		storedFreq>>sf1;
		storedFreq.close();

		ofstream myfile("SignalFile_FIR.txt", ios::out);

                x=new int [sf1];
                y=new int [sf1];

		for(double i=0;i<=f1;i=i+(f1/sf1))
		{
	x[n]=sam.getNextSample();

	y[n]=(b[0]*x[n]) + (b[1]*x[n-1]) + (b[2]*x[n-2])  + (b[3]*x[n-3])  + (b[4]*x[n-4])  + (b[5]*x[n-5])  + (b[6]*x[n-6])  + (b[7]*x[n-7])  + (b[8]*x[n-8])  + (b[9]*x[n-9])  + (b[10]*x[n-10])  + (b[11]*x[n-11])  + (b[12]*x[n-12])  + (b[13]*x[n-13])  + (b[14]*x[n-14])  + (b[15]*x[n-15])  + (b[16]*x[n-16])  + (b[17]*x[n-17])  + (b[18]*x[n-18])  + (b[19]*x[n-19])  + (b[20]*x[n-20])  + (b[21]*x[n-21])  + (b[21]*x[n-21]) + (b[22]*x[n-22])  + (b[23]*x[n-23])  + (b[24]*x[n-24])  + (b[25]*x[n-25])  + (b[26]*x[n-26])  + (b[27]*x[n-27])  + (b[28]*x[n-28])  + (b[29]*x[n-29]) + (b[30]*x[n-30]);
	myfile<<n<<"     "<<y[n]<<"\n";;
	n++;
		}
		myfile.close();
		n=0;
	}

	void print()
	{
		cout<<"\nCutoff Freq in Hz = 1.000000e+004\n";
		cout<<"Transition Band in Hz = 5.000000e+003\nStop Band Attenuation in dBs = -6.000000e+001\nNumber of FIR coeffs = 31\n";
		cout<<"\nFilter coefficients.\nIndex Coeff.s\n"<<endl;
		for(int i=0;i<=30;i++)
		{
			cout<<i<<"     "<<b[i]<<endl;
		}
	}

private:
double b[30],stest;
double* x;
double* y;
static int n;
char* readfile;
int f1,sf1;
Sampledwave sam;
SineWave sine;
};


My program wont compile because when creating the new arrays or even just one of them the values in the first for loop become wrong i.e sf1=0 f1=1076101120

The original values in this case should be f1=15000 sf1=40000.

sf1 and f1 come from a file and there file changes value. The size of the arrays needed will overflow the stack so that is why I need dynamic arrays.

I tried use a vector but same error.

The program compiles if I set arrays x and y to an initial value i.e. in private declare int x[30],y[30] and remove the new ints. But these value are only to test it.


Can anybody fix this problem.
Last edited on
What is n? I can't see where it's declared.
In the main program it is set to zero. FIR_filter is the class.

int FIR_filter::n=0;

Here I just show one function in the class and its private members.I can post all my code if needed.

Error got:
Unhandled exception at 0x01342c4d in ca1parts.exe: 0xC0000005: Access violation writing location 0x000000f0.


Looking at y in Autos it has this error:
y 0x00000000 double * CXX0030: Error: expression cannot be evaluated


Just note that if I remove the for loop the program compiles with no errors.


-

Last edited on
isn't
for(double i=0;i<=f1;i=i+(f1/sf1))
an infinate loop?
You have created a 30-element array:
double b[30]

And in the for loop you are accessing the 31st element of it:
b[30]


Here, too:
1
2
3
4
for(int i=0;i<=30;i++)
{
cout<<i<<" "<<b[i]<<endl;
}


Hint: You can make the code much shorter by using nested for loops.

BTW: Why C++ and not MATLAB? MATLAB would point you exactly to the line, where you have a bug.
Last edited on
How do you know if your indexing into arrays x and y are correct? I can't see where the guarantee is that n >= 30.
Last edited on
reply to Mooce
f1=15000 and sf1=40000 so it will loop for 40000 times in this case.
But x=new int [f1]; and/or y=new int [f1]; sets these to sf1=0 f1=1076101120.
I dont know why.

reply to Mooce
I change array b to b[31].. b does have 31 coeffs.
The for loop is displaying the 31 coeefs.
I have to use c++ because it is an assignment for college.

reply to kbw
In the main program it is initialised to zero. FIR_filter is the class.
int FIR_filter::n=0;
Ok here is my full code so ye can look at it

The file a_b_coeff.txt has the following coeffs stored in it
 
-4.008966e-004 7.644686e-004 8.084927e-004 -3.649528e-003 2.232512e-003 6.247866e-003 -1.188778e-002 3.022512e-016 2.333917e-002 -2.451241e-002 -1.819363e-002 6.613097e-002 -3.661643e-002 -1.075111e-001 2.907484e-001 6.250000e-001 2.907484e-001 -1.075111e-001 -3.661643e-002 6.613097e-002 -1.819363e-002 -2.451241e-002 2.333917e-002 3.022512e-016 -1.188778e-002 6.247866e-003 2.232512e-003 -3.649528e-003 8.084927e-004 7.644686e-004 -4.008966e-004


DSPfilter.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifndef dspFILTER_H
#define dspFILTER_H
#include <iostream>
#include<math.h>
#include<fstream>
#include <string.h>



using namespace std;


class Signal
{
	friend class FIR_filter;
public:
	Signal(){};
	Signal(int f,int a)
		:frequency(f),amp(a){};	
	
	virtual void load_file()=0;
	
	
protected:
	int frequency;
	int amp;
};


class SineWave: public Signal
{
	friend class FIR_filter;
public:
	
	SineWave(int f=0,int a=0,int sf=0)
		:Signal(f,a),samplingFreq(sf){};

	
	
	void load_file()
	{
		ofstream storedFreq("stored_f_sf.txt", ios::out);
		f0=frequency;
		sf0=samplingFreq;
		Pi=3.14159265;
		storedFreq<<f0<<" "<<sf0;
		storedFreq.close();

		ofstream SineWrite("SaveFile.txt", ios::out);
		for(double i=0;i<=f0;i=i+(f0/sf0))
		{
			
			SineWrite<<amp*sin(2*Pi*f0*t)<<" ";
			t=t+(1/sf0);
			count++;		
		}
		SineWrite.close();
		
	}

	int get_samplingFreq(){return samplingFreq;}
	

protected:
int samplingFreq;
double f0,sf0;
static int count;
static double t;
double x;
double Pi;
};

class Sampledwave:public SineWave
{
		friend class FIR_filter;
public:
	
	Sampledwave()
	{
	
	ifstream myfile("SaveFile.txt");
	if (!myfile.fail())
		{
		for (int i = 0;(!myfile.eof()); i++)
			{
				myfile >> sr[i];
			}
		myfile.close();
		
		}
	else
	{
		cout<<"Read SaveFile.txt Fail";
	}

	}

	
	int getNextSample(){return sr[sample1++];}
	
private:
	char* readfile;
	double sample;
	double sr[40000];
	int value;
	static int sample1;
	
};


class Filter
{
public:
	virtual void clock()=0;
	virtual	void get_Type()=0;
	virtual void print()=0;
protected:
};


class FIR_filter: public Filter
{
public:		
	FIR_filter(){
	
	ifstream myfile("a_b_coeff.txt");
	if (!myfile.fail())
		{
		for (int i = 0;(!myfile.eof()); i++)
			{
				myfile>>b[i];
			}
		myfile.close();
			
		}
	else
		{
		cout<<"Read a_b_coeff.txt Fail";
		}

	}

	void get_Type(){cout<<"Low Pass\n";}

	void clock()
	{
		ifstream storedFreq("stored_f_sf.txt");
		storedFreq>>f1;
		storedFreq>>sf1;
		storedFreq.close();

		ofstream myfile("SignalFile_FIR.txt", ios::out);
		x=new int[sf1];
		y=new int[sf1];
		for(double i=0;i<=f1;i=i+(f1/sf1))
		{
	x[n]=sam.getNextSample();
	y[n]=(b[0]*x[n]) + (b[1]*x[n-1]) + (b[2]*x[n-2])  + (b[3]*x[n-3])  + (b[4]*x[n-4])  + (b[5]*x[n-5])  + (b[6]*x[n-6])  + (b[7]*x[n-7])  + (b[8]*x[n-8])  + (b[9]*x[n-9])  + (b[10]*x[n-10])  + (b[11]*x[n-11])  + (b[12]*x[n-12])  + (b[13]*x[n-13])  + (b[14]*x[n-14])  + (b[15]*x[n-15])  + (b[16]*x[n-16])  + (b[17]*x[n-17])  + (b[18]*x[n-18])  + (b[19]*x[n-19])  + (b[20]*x[n-20])  + (b[21]*x[n-21])  + (b[21]*x[n-21]) + (b[22]*x[n-22])  + (b[23]*x[n-23])  + (b[24]*x[n-24])  + (b[25]*x[n-25])  + (b[26]*x[n-26])  + (b[27]*x[n-27])  + (b[28]*x[n-28])  + (b[29]*x[n-29]) + (b[30]*x[n-30]);
	myfile<<n<<"     "<<y[n]<<"\n";
	n++;
		}
		myfile.close();
		n=0;
	}

	void print()
	{
		cout<<"\nCutoff Freq in Hz = 1.000000e+004\n";
		cout<<"Transition Band in Hz = 5.000000e+003\nStop Band Attenuation in dBs = -6.000000e+001\nNumber of FIR coeffs = 31\n";
		cout<<"\nFilter coefficients.\nIndex Coeff.s\n"<<endl;
		for(int i=0;i<=30;i++)
		{
			cout<<i<<"     "<<b[i]<<endl;
		}
	}

private:
double b[31];
int* x;
int* y;
static int n;
char* readfile;
int f1,sf1;
Sampledwave sam;

};
#endif 



main.cpp
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
#include <iostream>
#include<math.h>
#include "DSPfilter.h"
#include<fstream>
#include <string.h>

int SineWave::count=0;
int FIR_filter::n=0;
double SineWave::t=0;
int Sampledwave::sample1=0;


using std::cout;
using namespace std;
using std::endl;

int main()
{	cout<<"Loading .....\n\n";
	SineWave s1(15000,10,40000);
	s1.load_file();
	Sampledwave sw1;
	FIR_filter fir1;
	cout<<"FIR\nType =";
	fir1.get_Type();
	cout<<"Sampling Freq in Hz ="<<s1.get_samplingFreq();
	fir1.print();
	fir1.clock();

	return 0;

}
Last edited on
please use code tags for this long code... place around it, or mark all the code and pick the '#' on the right side ("Format").
Your indexes are out of bounds, can't you see it?

What is x[n-29] when n is zero?
Reply to Incubbus
Sorry this is my first topic on this site. Fixed now.

Reply to kbw
I assumed it would be zero.
Could initialize x[n-1] to x[n-30] to zero or is that possible? i.e. for loop

Or do something like this:
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
if(n=0)
{
y[n]=(b[0]*x[n]) ;
}

else if(n=1)
{
y[n]=(b[0]*x[n]) + (b[1]*x[n-1]) ;
}
.
.
.
.

else if(n=10)
{
y[n]=(b[0]*x[n]) + (b[1]*x[n-1]) + (b[2]*x[n-2]) + (b[3]*x[n-3]) + (b[4]*x[n-4]) + (b[5]*x[n-5]) + (b[6]*x[n-6]) + (b[7]*x[n-7]) + (b[8]*x[n-8]) + (b[9]*x[n-9]) + (b[10]*x[n-10]) ;
}
.

.
.
.
else if(n>=30)
{
y[n]=(b[0]*x[n]) + (b[1]*x[n-1]) + (b[2]*x[n-2]) + (b[3]*x[n-3]) + (b[4]*x[n-4]) + (b[5]*x[n-5]) + (b[6]*x[n-6]) + (b[7]*x[n-7]) + (b[8]*x[n-8]) + (b[9]*x[n-9]) + (b[10]*x[n-10]) + (b[11]*x[n-11]) + (b[12]*x[n-12]) + (b[13]*x[n-13]) + (b[14]*x[n-14]) + (b[15]*x[n-15]) + (b[16]*x[n-16]) + (b[17]*x[n-17]) + (b[18]*x[n-18]) + (b[19]*x[n-19]) + (b[20]*x[n-20]) + (b[21]*x[n-21]) + (b[21]*x[n-21]) + (b[22]*x[n-22]) + (b[23]*x[n-23]) + (b[24]*x[n-24]) + (b[25]*x[n-25]) + (b[26]*x[n-26]) + (b[27]*x[n-27]) + (b[28]*x[n-28]) + (b[29]*x[n-29]) + (b[30]*x[n-30]);
}


Or instread of if statements use cases?
Last edited on
Solved

moooce was correct in saying
isn't
for(double i=0;i<=f1;i=i+(f1/sf1))
an infinate loop?


I declare sf1 and f1 as int's so (f1/sf1)=0 i.e (15000/40000)=0
I just changed them to double's

I used the if else statements above to solve the x[n-1] to x[n-30] when n<=30 problem

And then just one more problem needed to be changed.
1
2
3
4
5
int int_sf1;
int_sf1=sf1;

x=new int[int_sf1]; //array cannot be a double value
y=new int[int_sf1];

Topic archived. No new replies allowed.