September 11th, 2003, 02:27 PM
C++ & C casting - segmentation fault
I am having some problems porting a C program (traffic simulator) to C++ using the g++ compiler.
For a car structure defined in C header file :
typedef struct /* type definition for cars */
int id; /*car identifier*/
double distance; /* distance travelled so far */
double speed; /* speed of the car */
int x,y; /* graphics: x & y coords of the car */
void * nextlane; /* the lane we will enter in the next junc */
void * behind; /* pointer to car behind in the lane */
which I included in the C++ program as follows:
I need to do a casting in my C++ main program, as follows, before it compiles without errors:
c = (car_t *) fc->behind;
but when I am running the simulation it is giving me a segmentation fault where I am using car c
for example : c->distance
It does not give that error when the program first goes through the simulation loop i.e. only when it goes to the next car after the casting step.
I think there is a problem with the way I am casting or I have missed some steps somewhere.
Any help will be appreciated as this has already took too much of my time.