| | |
EAccess Violation in small game
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 6
Reputation:
Solved Threads: 0
Hi guys, i have been having problems with this piece of code all afternoon, i have never seen this error myself before and cant see how the problem is applicable in my situation from looking though the various forums of the world !
Basically the program is a small game where there are two cars that race each other to a pre defined point on the page, my problem lies in a small piece of code that updates the position of a car.
The error is detailed as: "Debugger exception notice: Project Drag.exe raised exception class EAccess violation. Access violation at adress 00401e13 in module drag.exe. Read of address 00000000".
I have read about null pointers but i dont not completely understand how they work with vectors, any help would be appreciated !
The vector is created in the TMainForm, using
This section shows the stopwatch that is set to execute the race itself
While there is no error shown in this section the code is related to it so i thoguht i should show it anyway...
Sorry for the info overload !
Sam
Basically the program is a small game where there are two cars that race each other to a pre defined point on the page, my problem lies in a small piece of code that updates the position of a car.
The error is detailed as: "Debugger exception notice: Project Drag.exe raised exception class EAccess violation. Access violation at adress 00401e13 in module drag.exe. Read of address 00000000".
I have read about null pointers but i dont not completely understand how they work with vectors, any help would be appreciated !
The vector is created in the TMainForm, using
C++ Syntax (Toggle Plain Text)
private: // User declarations public: // User declarations std::vector<RacingCar*> cars ;
This section shows the stopwatch that is set to execute the race itself
void __fastcall TMainForm::StopWatchTimer(TObject *Sender)
{
TTimer* timer = static_cast<TTimer*>(Sender);
int dt = timer->Interval / 1000;
{ // drive the cars
for(int i = 0; i != cars.size(); ++i)
cars[i]->Drive(dt);
int i = 0;
// update the race track
Image2->Left += cars[i++]->DistanceTravelled(); /*This line is the one
causing the error, when commented out rogram works fine.*/
}
}While there is no error shown in this section the code is related to it so i thoguht i should show it anyway...
C++ Syntax (Toggle Plain Text)
void RacingCar::Drive(unsigned int time) { //Determine when the winner has got tot he finish line ands make them stop if (mileometer <=1200) { // work our speed increase unsigned int speed_inc = Acceleration() * time; // check maximum speed is not exceeded if(speed + speed_inc > MaximumSpeed()) speed_inc = MaximumSpeed() - speed; // calculate distance travelled mileometer += (speed + speed_inc) / 2 * time; Oil = random(100); if (Oil == 50) { speed = 0; speed_inc = 0; } // update speed speed += speed_inc; // apply drag if(Drag() > speed) speed = 0; else speed -= Drag(); } else { speed = 0; winner = mileometer; } }
Sorry for the info overload !
Sam
•
•
Join Date: Jan 2009
Posts: 6
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
RacingCar::RacingCar() :mileometer(0) ,speed(0) ,Oil(0) { }
C++ Syntax (Toggle Plain Text)
class RacingCar { public: RacingCar(); void Drive(unsigned int time); unsigned int DistanceTravelled() const; unsigned int Winner() const; private: unsigned int Oil; unsigned int mileometer; unsigned int speed; unsigned int winner; virtual unsigned int Acceleration() const = 0; virtual unsigned int MaximumSpeed() const = 0; virtual unsigned int Drag() const = 0; } ;
Basically i have a copy of a program that is similar to mine that complies that was given to me by a lecturer but he is external so i havent seen him for 2-3 week so i cant get any help off him :/
•
•
Join Date: Mar 2009
Posts: 53
Reputation:
Solved Threads: 4
Image2->Left += cars[i++]->DistanceTravelled();
Is I always supposed to be 1? Looking through the code you posted, it seems that I will always end up being 1. Is there a value in the vector cars at position 1?
One thing you can do to debug this would be to do the following:
cars[i++]->DistanceTravelled();
if you put this on a line by itself and it still crashes, the error occurs during accessing the vector. If it doesn't, then the error occurs when you try to assign a value to Image2->Left.
Is I always supposed to be 1? Looking through the code you posted, it seems that I will always end up being 1. Is there a value in the vector cars at position 1?
One thing you can do to debug this would be to do the following:
cars[i++]->DistanceTravelled();
if you put this on a line by itself and it still crashes, the error occurs during accessing the vector. If it doesn't, then the error occurs when you try to assign a value to Image2->Left.
•
•
Join Date: Jan 2009
Posts: 6
Reputation:
Solved Threads: 0
Yeah the error is in accesing the vector, i kind of assumed this was the problem from the start...
To answer the 1st question there is Image3->Left +=cars[i++]->DistanceTravelled() , the i++ basically acts to move on to the next car unless i have completely mis understood the way the program works from my lecturer.... xD
The Accelleration speed etc all come from a seperate unit (e.g. Ferrari.cpp) and since i have the program working in a different place/execution know the concept is fairly sound...
To answer the 1st question there is Image3->Left +=cars[i++]->DistanceTravelled() , the i++ basically acts to move on to the next car unless i have completely mis understood the way the program works from my lecturer.... xD
The Accelleration speed etc all come from a seperate unit (e.g. Ferrari.cpp) and since i have the program working in a different place/execution know the concept is fairly sound...
![]() |
Similar Threads
- Hiii am an amateur programmer.. Want a MEMORY GAME (C++)
- Socket Server for game - too many threads? (Python)
- Need manpower to create game program (Game Development)
- C++ game? (C++)
- school project: simple C++ game (need help with keyboard input) (C++)
- this is a small game using mouse (C++)
- No Connectivity Through Network (Networking Hardware Configuration)
- 10 line text file (Java)
Other Threads in the C++ Forum
- Previous Thread: Check whether 2 Arrays contain the same values
- Next Thread: String arrays
| 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 developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





