| | |
pointer array went wrong
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I've got an array of pointers to my class "Rule" Now I'm trying to access it from within a member function AddRule. But on the first line I keep getting "Memory Access Violation"
Can anyone tell what's wrong?
Can anyone tell what's wrong?
C++ Syntax (Toggle Plain Text)
class RuleSet { private: public: Rule *Rules[200]; int RuleNum; void AddRule(Rule); void ProcessCell(int); RuleSet(); }; void RuleSet::AddRule(Rule A) { Rules[RuleNum]->min.MembraneOut[0]=A.min.MembraneOut[0]; // !!! THE BAD LINE RuleNum++; } RuleSet::RuleSet() { this->RuleNum=0; int i; for (i=0;i<200;i++) { Rules[i]=new(Rule); } };
Quick nickpicks: I don't see a destructor, and keep rules[] private. Normally variable names are lowercase.
Why are you assigning it zero?
Why are you assigning it zero?
Rule *rules; rules = new Rule [200]; delete [] rules;
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
C++ Syntax (Toggle Plain Text)
class Rule { public: LivingCell min; LivingCell max; LivingCell IgnoredParametres; int Action; int Parameter; }; // ------------------- Класс "Генотип" ----------------- class RuleSet { private: Rule *Rules; public: int RuleNum; void AddRule(Rule); void ProcessCell(int); RuleSet(); }; RuleSet *Genome; // ------------------- Функции класса "Генотип" ----------------- void RuleSet::AddRule(Rule A) { Rules[this->RuleNum].min.MembraneOut[0]=A.min.MembraneOut[0]; // That's the bad line this->RuleNum++; } RuleSet::RuleSet() { this->RuleNum=0; int i; Rules=new Rule[200]; };
Note that
this->RuleNum actually doesn't work either (value is ????)But even if I change it into a constant (0 for instance) I've got no effect - still "Memory Access Violation"
wouldn't it have to be
Chris
C++ Syntax (Toggle Plain Text)
Rules[this->RuleNum]->min->MembraneOut[0]=A.min.MembraneOut[0];
Chris
Knowledge is power -- But experience is everything
•
•
Join Date: May 2008
Posts: 614
Reputation:
Solved Threads: 97
Show us the code where you create a RuleSet and then where you add a Rule to the RuleSet.
If you don't add any code and then call
Did you mean somewhere to do
RuleSet * Genome declares a pointer to a RuleSet but never actually creates one.If you don't add any code and then call
Genome->AddRule(...) Genome is an undefined pointer (probably NULL) and will fault in the manner described.Did you mean somewhere to do
Genome = new RuleSet(); or would you just prefer to declare it: RuleSet Genome; . ![]() |
Similar Threads
- Return pointer to array of a struct (C++)
- what's wrong with this use of new[] ? (C++)
- Array Length (C++)
- Little Simple array question :) (C)
- memory allocation ptr to array? how? (C)
- Declaration of dynamic pointer array puzzle. (C)
- Please help with pointer to array of pointers (C++)
- help with sort using Calendar class getting null pointer exception (Java)
Other Threads in the C++ Forum
- Previous Thread: C++ Network Developing Advice
- Next Thread: get/put(VARIANT newVal) ??
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





