EXC_BAD_ACCESS
Dec 30, 2011 at 5:51am UTC
Hey, I'm returning to c++ after taking the course in college, and I've run into a mysterious error: whenever I try to "set" a string member variable I get the EXC_BAD_ACCESS error. I'm developing with XCode 4.2 on Mac OS 10.7 (Lion) using compiler gcc 4.2.1.
Property.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <string.h>
class Property {
std::string sKey;
std::string sValue;
public :
Property();
Property(const char * key);
Property(const char * key, const char * value);
std::string & getKey();
std::string & getValue();
const char * getSZKey();
const char * getSZValue();
void setKey(std::string key);
void setValue(std::string value);
bool isForKey(const char * key) const ;
};
Property.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 <string.h>
#include "Property.h"
#include <iostream>
using namespace std;
Property::Property(): sKey("" ), sValue("" ) {
}
Property::Property(const char * key): sKey(key), sValue("" ) {
}
Property::Property(const char * key, const char * value): sKey(key), sValue(value) {
}
string & Property::getKey() {
return sKey;
}
string & Property::getValue() {
return sValue;
}
const char * Property::getSZKey() {
return this ->sKey.c_str();
}
const char * Property::getSZValue() {
return this ->sValue.c_str();
}
void Property::setKey(string key) {
sKey = key;
}
void Property::setValue(string value) {
sValue = value;
}
bool Property::isForKey(const char * key) const {
return strcmp(key,sKey.c_str()) == 0;
}
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 32 33 34 35 36 37
#include <vector>
#include <iostream>
#include "Property.h"
using namespace std;
void tokenize(const string& str,
vector<string>& tokens,
const string& delimiters = " " ) {
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos) {
tokens.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiters, pos);
pos = str.find_first_of(delimiters, lastPos);
}
}
int main (int argc, const char * argv[]) {
vector<Property> propertyVector(argc - 1);
vector<string> argumentTokens;
const string delimiters("=" );
for (int i = 1; i < argc; i++) {
string argument(argv[i]);
tokenize(argument, argumentTokens, delimiters);
switch (argumentTokens.size()) {
case 2:
propertyVector[i].setKey(argumentTokens[0].c_str());
propertyVector[i].setValue(argumentTokens[1].c_str());
break ;
default :
break ;
}
argumentTokens.clear();
}
return 0;
}
This is a reduced version of my program's problem. The objective is simply to read in arguments of "key=value" and create Property objects with the key and value in the data structure. The error occurs on line(s) 25 and 28 of Property.cpp. I really don't understand what's happening. What am I doing wrong? It must be something simple. Please help!
Last edited on Dec 30, 2011 at 6:01am UTC
Dec 30, 2011 at 2:03pm UTC
Array index goes from 0 to n-1
Dec 30, 2011 at 6:31pm UTC
THAT FIXED IT! I can't believe it was that simple. Well, that's what I get for programming after 8pm. Thank you, my friend.
Topic archived. No new replies allowed.