What's wrong?
trynreadme (21)
Nov 2, 2012 at 4:04am UTC
1 2 3 4 5 6 7 8 9 10 11 12
string GLOBAL_arrayInventory[0];
int GLOBAL_intItemNumber = 0;
void addItem(string);
addItem(GLOBAL_tempNewItem);
// BEGIN ADD ITEM
void addItem(string tempNewItem){
GLOBAL_arrayInventory[GLOBAL_intItemNumber] = tempNewItem;
GLOBAL_intItemNumber += 1;
}
// END ADD ITEM
What's wrong with this function? I don't understand what's happening here. I'm getting:
0x7fff9067ebb3: movb (%r14), %cl Thread 1: EXC_BAD_ACCESS (code=1, address=0x10aba03f8)
I don't understand why I'm getting this error.
Zhuge (2871)
Nov 2, 2012 at 4:12am UTC
Well it seems you have declared an array of zero length (I'm confused as to why this is even legal, but whatever), and you are trying to access one of its elements. Since it has zero length it has no elements and attempting to accessing one results in the error you are getting.
trynreadme (21)
Nov 2, 2012 at 5:44am UTC
unfortunately still got the same error.
I'm able to initialize another array in a similar way.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
string GLOBAL_arrayItems[0];
void initArrayItems();
initArrayItems();
void initArrayItems(){
GLOBAL_arrayItems[0] = "Hammer" ;
GLOBAL_arrayItems[1] = "Sword" ;
GLOBAL_arrayItems[2] = "Vasoline" ;
GLOBAL_arrayItems[3] = "Bow" ;
GLOBAL_arrayItems[4] = "+5 Health" ;
GLOBAL_arrayItems[5] = "+10 Health" ;
GLOBAL_arrayItems[6] = "Axe" ;
}
Last edited on Nov 2, 2012 at 5:51am UTC
trynreadme (21)
Nov 2, 2012 at 7:22am UTC
Now when I run an inventory check with more than one item in the inventory I get this:
1 2 3
Inventory:
+5 Health
h({\202x\377+5 HealthordVa
1 2 3 4 5 6 7 8 9 10 11
// BEGIN CHECK INVENTORY
void checkInventory(){
int tempItemNumber = 0;
tempItemNumber = GLOBAL_intItemNumber;
cout << "Inventory:" << endl;
while (tempItemNumber > 0){
cout << GLOBAL_arrayInventory[tempItemNumber] << endl;
tempItemNumber -= 1;
}
}
// END CHECK INVENTORY
I changed the initArrayItems back the way I originally had it, in the randomItemCreator, and it seems to be fine now. Aside from the issue I'm having above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// BEGIN RANDOM ITEM CREATOR
string randomItemCreator(){
int randomItemSelector = 0;
string randomItemReturn = "" ;
GLOBAL_arrayItems[1] = "Hammer" ;
GLOBAL_arrayItems[2] = "Sword" ;
GLOBAL_arrayItems[3] = "Vasoline" ;
GLOBAL_arrayItems[4] = "Bow" ;
GLOBAL_arrayItems[5] = "+5 Health" ;
GLOBAL_arrayItems[6] = "+10 Health" ;
GLOBAL_arrayItems[7] = "Axe" ;
srand(static_cast <int >(time(0)));
randomItemSelector = 1 + rand() % (7 - 1 + 1);
randomItemReturn = GLOBAL_arrayItems[randomItemSelector];
return randomItemReturn;
}
// END RANDOM ITEM CREATOR
Thanks in advanced.