Memory Access Violation

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

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

Memory Access Violation

 
0
  #1
Jan 7th, 2009
I quite don't get it!

I've got a memory access violation on a perfectly normal code string. Error occurs when starting the application. I can click continiuse and everything goes on ok.
And more. If I enter a breakpoint before the bad line it doesn't stop there but the error is gone!

The function I'm referring to doesn't run on startup, it uses no pointers or smth. So as I told, I don't get it.

Can anyone help. Please!

  1. int Field::GetCellX(int x, int y)
  2. {
  3. int i,ii,cx,cy;
  4. double dist;
  5. double mindist;
  6. mindist=10000;
  7. int CellX,CellY;
  8.  
  9. for (i=0;i<=(this->Picture->Width/(3*this->cellsize));i++)
  10. for (ii=0;ii<=(this->Picture->Height/(4*this->cellsize));ii++)
  11. {
  12. cx=4*this->cellsize/2+i*3*this->cellsize;
  13. cy=(((i%2)==0)?(4*this->cellsize/2+ii*4*this->cellsize):(4*this->cellsize+ii*4*this->cellsize));
  14. dist=((cx-x)*(cx-x)+(cy-y)*(cy-y));
  15. if (dist<mindist)
  16. {
  17. CellX=i;
  18. CellY=ii;
  19. mindist=dist; // !!!! THIS IS THE BAD LINE !!!!
  20. };
  21. }
  22. return CellX+this->x;
  23. };

Here is class declaration. Just in case...

  1. class Field
  2. {
  3. public:
  4. LivingCell *cell[GameSize*10];
  5. FieldElement *point[GameSize][GameSize];
  6. int density[6];
  7. TImage *Picture;
  8. int cellsize;
  9. int x,y;
  10.  
  11. Field();
  12. void DrawField();
  13. int GetCellX(int x, int y);
  14. int GetCellY(int x, int y);
  15. void Spread(int, int, int);
  16. void ClearAgents(void);
  17. };
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Memory Access Violation

 
0
  #2
Jan 7th, 2009
>>for (i=0;i<=(this->Picture->Width/(3*this->cellsize));i++)
>> for (ii=0;ii<=(this->Picture->Height/(4*this->cellsize));ii++)

Don't you realize the program has to make all those calculations on every loop iteration? That's very very time consuming since the value of Width and cellsize do not change during those loops. It would be much more efficient to just calculate them once above/outside those two loops and use the temp variables inside the loop.

As for your problem: I suspect memory corruption somewhere else. Your program has probably done something to corrupt stack, and the problem just happens to appear on the line you marked. My suggestion is to start commenting stuff out until the problem disappears.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Memory Access Violation

 
0
  #3
Jan 7th, 2009
Obviously you can't get access violation at this statement. Possible cause: stack corruption (for example, overwriting of return address) or bad (uninitialized) pointer in your code (outside of GetCellX function call). Try to go through step by step with debugger.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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