| | |
Memory Access Violation
Please support our C++ advertiser: Intel Parallel Studio Home
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 beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






