Exc_Bad_Access

Hello,

First of all, this place is great. With one exception, I have always been able to find information leading me to my answer by browsing through the forums and tutorials. The one exception I will detail below.

I will be honest, this is a homework question, but I am NOT looking for the answer. I have already written the code, I am an astute student, I refuse to share my homework with other students etc, but I am having problems with this one instance. I would ask my professor, but she is impossible to understand, and I've noticed her make a few errors during class, so I'd rather speak to the experts here hehe.

Basically, I need to input baby names from a file into an array (I did three arrays; one each for the rank, boys names, and girls names), then allow the user to search for any name. The result returned to the user is the ranking of their query among Boys names and among Girls names. If the name is not found, I also display that to the console for the user. I am using XCode 3.2.6.


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
#include <iostream>
#include <fstream>
using namespace std;

void getInput();
void getBoysNameRank();
void getGirlsNameRank();

string userInput;
string boys[1000];
string girls[1000];
int rank[1000];
bool boysFound = false;
bool girlsFound = false;

ifstream input;

int main() {

	getInput();
	getBoysNameRank();
	getGirlsNameRank();
	
	return 0;

}

void getInput() {

	input.open("babynames.txt");

	for(int i=0; i<1001; i++) {
		input >> rank[i];
		input >> boys[i];
		input >> girls[i];
	}

	cout << "Please input your query: ";
	cin >> userInput;
	
	input.close();
}


void getBoysNameRank() {
	int position;
	for(int index=0; index<1001; index++) {
		if (userInput == boys[index]) {
			position = index;
			boysFound = true;
		}				
	}
	
	if (boysFound)
		cout << userInput << " is ranked " << position << " in popularity among boys names." << endl;
	if (!boysFound)
		cout << userInput << " is not among the top 1000 boys names." << endl;
}



void getGirlsNameRank() {
	int position;
	for(int index=0; index<1001; index++) {
		if (userInput == girls[index]) {
			position = index;
			girlsFound = true;
		}
	}

	if (girlsFound)
		cout << userInput << " is ranked " << position << " in popularity among girls names." << endl;
	if (!girlsFound)
		cout << userInput << " is not among the top 1000 girls names." << endl;
}


I've spent a lot of time fiddling and experimenting, but whatever I do, I cannot get the girls result to display with the boys result, and I get what seems like an infinite loop...


Example:
"Please input your query: " Walter

Output:
"Walter is ranked 365 among boys names."
Program received signal: EXC_BAD_ACCESS
(gdb)



I then have to Terminate the program in order to stop it, whereas it should terminate right after printing the results.

I tried removing all the code pertaining to the "getGirlsNameRank()" function, and the program has zero errors, no EXC_BAD_ACCESS or anything.

So I researched this EXC_BAD_ACCESS, which let me know that it is either an object not initialized, an object already released, or "something else that is not very likely to happen" (http://www.touch-code-magazine.com/how-to-debug-exc_bad_access/). I am brand new to C++, just started learning at the beginning of May, and I am just not sure what I am doing wrong. I read that I can use Zombies to fix the releasing problem, but I am unclear how to apply that to my situation, and as we have not learned anything about that yet, I assume that it is not the fix to my problem.

Is it saying that I cannot reuse the "userInput" variable for my getGirlsNameRank() function? I flipped the order of the getGirlsNameRank and the getBoysNameRank in the int main() function call, but then the program would not output anything.

Or is it something to do with passing values between functions? I tried using references to pass values (in the prototype, the call, and the definition) for the getGirlsNameRank function, then for the whole program, which did not fix it. I also tried simplifying the getBoysNameRank and getGirlsNameRank functions by removing any cout statements and the boolean tests, but that still didn't fix my problem.

Any help is appreciated. If this is deemed too close to helping me with my homework, I understand. Thank you for your time anyway.
If an array has 1000 elements, the valid indices will run from 0 to 999. Your for loops have the possibility to go up to 1000, which would be an invalid index for the array and out of bounds.
An example of the problem with using namespace std; ?
main.cpp:33:18: error: reference to 'rank' is ambiguous
input >> rank[i];
^
main.cpp:12:5: note: candidate found by name lookup is 'rank'
int rank[1000];
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/type_traits:1326:12: note:
candidate found by name lookup is 'std::rank'
struct rank
^
1 error generated.
Wow, I guess that is what happens when you have stared at a screen for far too long. Thank you very much for fixing my error!

My professor put an example on the board yesterday (yes, she writes all the programs on a blackboard, instead of in an IDE where she could run the example and show us step by step what is happening), specifically mentioning that you should err on the side of caution and increase the for loop by one. *Sigh*

I will declare a const int SIZE = 1000 and just use SIZE throughout the function. Again, thank you!
@norm B I didn't even know there could be conflict issues with the std namespace. That seems like a whole bag of worms more difficult to debug, so I am glad that wildblue's solution fixed my error.
It may depend on your compiler if you get an error on rank.

To fix that, you'd just remove the using namespace std;
and add prefix std:: to your cin, cout, endl and string

or - just give the rank array a different name. But in general, it's a better idea I think not to use namespace std. That pulls in a lot of things that you may not be aware of.
Last edited on
I did not know that, thank you! So much for teaching the basics correctly. I'm learning more from you and this website. I should just drop out, but that piece of paper at the end of the degree is useful...

Is there an alternate namespace that would be better to use? I will for sure be wary of the use of the word "rank" in future.
std::rank() was added in the c++11 standard.

wildblue is correct using using namespace std; is a bad habit to get into. Best to nip it in the bud.

@Seag - I ran your code on my machine but now I notice that the new feature of the site (C++ shell) shows the same error. Click on the gear icon on the upper right of the code block.
Topic archived. No new replies allowed.