pointer array went wrong

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 8
Reputation: heimdhal is an unknown quantity at this point 
Solved Threads: 0
heimdhal's Avatar
heimdhal heimdhal is offline Offline
Newbie Poster

pointer array went wrong

 
0
  #1
Jan 8th, 2009
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?


  1. class RuleSet
  2. {
  3. private:
  4.  
  5. public:
  6. Rule *Rules[200];
  7. int RuleNum;
  8.  
  9. void AddRule(Rule);
  10. void ProcessCell(int);
  11. RuleSet();
  12. };
  13.  
  14. void RuleSet::AddRule(Rule A)
  15. {
  16. Rules[RuleNum]->min.MembraneOut[0]=A.min.MembraneOut[0]; // !!! THE BAD LINE
  17. RuleNum++;
  18. }
  19.  
  20. RuleSet::RuleSet()
  21. {
  22. this->RuleNum=0;
  23. int i;
  24. for (i=0;i<200;i++)
  25. {
  26. Rules[i]=new(Rule);
  27. }
  28. };
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 972
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: pointer array went wrong

 
0
  #2
Jan 8th, 2009
Quick nickpicks: I don't see a destructor, and keep rules[] private. Normally variable names are lowercase.

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
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 8
Reputation: heimdhal is an unknown quantity at this point 
Solved Threads: 0
heimdhal's Avatar
heimdhal heimdhal is offline Offline
Newbie Poster

Re: pointer array went wrong

 
0
  #3
Jan 8th, 2009
Well I did like you told, but still it doesn't work.

And using watches I found that
RuleNum 's value is ???? (four question marks
)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 972
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: pointer array went wrong

 
0
  #4
Jan 8th, 2009
Can I see some code?
"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
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 8
Reputation: heimdhal is an unknown quantity at this point 
Solved Threads: 0
heimdhal's Avatar
heimdhal heimdhal is offline Offline
Newbie Poster

Re: pointer array went wrong

 
0
  #5
Jan 8th, 2009
  1. class Rule
  2. {
  3. public:
  4. LivingCell min;
  5. LivingCell max;
  6. LivingCell IgnoredParametres;
  7. int Action;
  8. int Parameter;
  9. };
  10. // ------------------- Класс "Генотип" -----------------
  11. class RuleSet
  12. {
  13. private:
  14. Rule *Rules;
  15. public:
  16.  
  17. int RuleNum;
  18.  
  19. void AddRule(Rule);
  20. void ProcessCell(int);
  21. RuleSet();
  22. };
  23.  
  24. RuleSet *Genome;
  25.  
  26. // ------------------- Функции класса "Генотип" -----------------
  27. void RuleSet::AddRule(Rule A)
  28. {
  29. Rules[this->RuleNum].min.MembraneOut[0]=A.min.MembraneOut[0]; // That's the bad line
  30. this->RuleNum++;
  31. }
  32.  
  33. RuleSet::RuleSet()
  34. {
  35. this->RuleNum=0;
  36. int i;
  37. Rules=new Rule[200];
  38. };

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"
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: pointer array went wrong

 
0
  #6
Jan 8th, 2009
wouldn't it have to be
  1. Rules[this->RuleNum]->min->MembraneOut[0]=A.min.MembraneOut[0];

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 8
Reputation: heimdhal is an unknown quantity at this point 
Solved Threads: 0
heimdhal's Avatar
heimdhal heimdhal is offline Offline
Newbie Poster

Re: pointer array went wrong

 
0
  #7
Jan 8th, 2009
No It wouldn't.
Rules[SomeInt] is a class, not a pointer to one.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: pointer array went wrong

 
0
  #8
Jan 8th, 2009
oh sorry lol i read you had created an array of pointers...then just glanced over the code...but you hadn't created and array of pointers
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 8
Reputation: heimdhal is an unknown quantity at this point 
Solved Threads: 0
heimdhal's Avatar
heimdhal heimdhal is offline Offline
Newbie Poster

Re: pointer array went wrong

 
0
  #9
Jan 8th, 2009
watches also told me that in function in question
this = NULL

That's supposed to be a member function!

I just don't get it...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 614
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 97
Murtan Murtan is offline Offline
Practically a Master Poster

Re: pointer array went wrong

 
0
  #10
Jan 8th, 2009
Show us the code where you create a RuleSet and then where you add a Rule to the RuleSet.

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; .
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC