EXC_BAD_ACCESS (code=1, address=0x0)

Apr 14, 2020 at 11:19am
Hello, I am very new to C++ and have this error message with my code. EXC_BAD_ACCESS (code=1, address=0x0). i am more comfortable with using codeblock at my school library but now i have to use xcode on my mac.
the issue happens at filterData[i] = (1/50) * sum; any help would be great thank you!

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
  #include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int countlines= 0;

void number(){
    countlines--;
    cout << "lines: " << countlines << endl;
};

int main(){
    vector<double> data;
    vector<double> filterData(countlines);
    double values, sum = 0.0, max;
    string line;
    int i, j, k;
   
    
    ifstream rawECGdata;
    ifstream RESPdata;
    rawECGdata.open("ecg.txt");
    RESPdata.open("resp.txt");
    
    if(!rawECGdata.is_open()){
        cout << "error cant open " << endl;
        }
    
    while(rawECGdata >> values){
        countlines++;
        data.push_back(values);
        }
    
    for (i=0; i<countlines; i++)
    {
        for (k=0; k<50; k++)
        {
            j = (i+k-25);
            if ((j > 0)&&(j<=countlines))
            {
                sum = sum + data[j];
            }
        }
        filterData[i] = (1/50) * sum;
    }
    
    max = *max_element(filterData.begin(),filterData.end());
    cout << "max value: " << max << endl;
}
Apr 14, 2020 at 11:44am
Move line 19 to line 38. At line 19 countlines is still zero, so you're creating an std::vector with zero elements, and then on line 49 you're accessing it out of bounds.

Also, 1/50 is exactly zero. You need to multiply sum by 1.0/50.0.
Apr 14, 2020 at 11:48am
Look at how you're initialising your filterData vector. How big is it actually going to be?

EDIT: Ninja'd.
Last edited on Apr 14, 2020 at 11:49am
Apr 14, 2020 at 11:51am
Thank you guys for the help moving, the code to line 38 and changing my 1/50 solved it.
Apr 14, 2020 at 12:32pm
Hello jtek679,

In addition to what has not be covered:

Line 52 used the function "max_element()". This is from the header file "<algorithm>" which you are missing.

Try to avoid using global variables. This may seem easy right not, but could be a bigger problem in the future.

The best I can tell the function "number" is never used.

You wrote:
1
2
3
4
5
6
7
8
9
10
if (!rawECGdata.is_open())
{
	cout << "error cant open " << endl;
}

while (rawECGdata >> values)
{
	countlines++;
	data.push_back(values);
}

Good start, but after you print the error message you continue with the while loop not being able to read anything.

What would work better is:
1
2
3
4
5
6
if (!rawECGdata)
{
	cout << "error cant open " << endl;

	return 1;  // <--- No point in continuing with the program.
}

This checks more than just the file being open. Then you leave the program to fix the problem. Since (0) zero means a normal return any number greater than zero denotes a problem.

That is what I see in the program.

Andy
Topic archived. No new replies allowed.