| | |
Memory Access Violation
Thread Solved
![]() |
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!
Here is class declaration. Just in case...
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!
C++ Syntax (Toggle Plain Text)
int Field::GetCellX(int x, int y) { int i,ii,cx,cy; double dist; double mindist; mindist=10000; int CellX,CellY; for (i=0;i<=(this->Picture->Width/(3*this->cellsize));i++) for (ii=0;ii<=(this->Picture->Height/(4*this->cellsize));ii++) { cx=4*this->cellsize/2+i*3*this->cellsize; cy=(((i%2)==0)?(4*this->cellsize/2+ii*4*this->cellsize):(4*this->cellsize+ii*4*this->cellsize)); dist=((cx-x)*(cx-x)+(cy-y)*(cy-y)); if (dist<mindist) { CellX=i; CellY=ii; mindist=dist; // !!!! THIS IS THE BAD LINE !!!! }; } return CellX+this->x; };
Here is class declaration. Just in case...
C++ Syntax (Toggle Plain Text)
class Field { public: LivingCell *cell[GameSize*10]; FieldElement *point[GameSize][GameSize]; int density[6]; TImage *Picture; int cellsize; int x,y; Field(); void DrawField(); int GetCellX(int x, int y); int GetCellY(int x, int y); void Spread(int, int, int); void ClearAgents(void); };
>>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.
>> 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.
![]() |
Similar Threads
- File Mapping Causing Access Violation (C++)
- Access violation (C++)
- Delphi Class memory access violation. Why??? (Pascal and Delphi)
- windows xp pro stop error, happens after only a few minutes of loading up (Troubleshooting Dead Machines)
- Access violation in C (C)
- Access Violation (Segmentation Fault) + atol (C++)
- unhandaled exception,0X000005:access violation (C)
Other Threads in the C++ Forum
- Previous Thread: what is going on in this function?
- Next Thread: Builder cpp 6 - resources problem
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes code coding compaitibility compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error factorial file floatingpoint forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple net news node number numbertoword output parameter payment pointer problem program programming project projectassignmenthelp protection python random rank read recursion reference rpg skills string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






