Sign in to follow this  

runtime error: Access violation

This topic is 3787 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im trying to write a physics simulation that simulates the result of various forces on particles, and in the latest version, it has been displaying the runtime error: Unhandled exception at 0x6c18320f in CoordinatesTest.exe: 0xC0000005: Access violation reading location 0x00000053. I tried just moving all of the graphics code out of the source file and running it, just displaying certain states of the particles, but it continued to display this message. I'm not incredibly experienced, so this may or may not be a simple problem, but any help is much appreciated. #include <stdio.h> #include <stdlib.h> #include <math.h> #define NUM_PARTICLES 9 #define EARTH_MASS 5.9742E+24 #define AU 149598000//kilometers #define GC 6.0E-14//pow(kilometer,3)/kg * pow(s, 2) #define PI 3.14159265358979323846264338327950 char names[NUM_PARTICLES] = {'S', 'M', 'V', 'E', 'm', 'J', 'S', 'U', 'N'}; double Points[NUM_PARTICLES][3]; double Velocities[NUM_PARTICLES][NUM_PARTICLES-1][3]; double finalVelocities[NUM_PARTICLES][3]; double force[3]={0.0, -9.8, 0.0};// force due to Earth's gravity double distances[NUM_PARTICLES][NUM_PARTICLES-1];//array of the distances between particles, first element is between first and second particle etc. double pGrav[NUM_PARTICLES][NUM_PARTICLES-1];//array of the gravity force between particles using distances[] to calculate it, and storing in the same manner double radius[NUM_PARTICLES] = {0.00 * AU, 0.39 * AU, 0.72 * AU, 1.00 * AU, 1.52 * AU, 5.20 * AU, 9.54 * AU, 19.22 * AU, 30.06 * AU}; double orbitalAngles[NUM_PARTICLES] = {0.00, 3.38, 3.86, 7.25, 5.65, 6.09, 5.51, 6.48, 6.43}; double elevation[NUM_PARTICLES]; double mass[NUM_PARTICLES] = {3329146 * EARTH_MASS,0.055 *EARTH_MASS, 0.815 * EARTH_MASS, 1.0 * EARTH_MASS, 0.107 * EARTH_MASS, 317.8 *EARTH_MASS, 95.152 * EARTH_MASS, 14.536 * EARTH_MASS, 17.147 * EARTH_MASS};//mass of the planets as multiples if Earth double time_step = 0.01; double time = 0.0; int x; double Elevation(double angle, double radius){ return (sin(angle*(PI/180)) * radius); } //3d distance formula double distance(double x1, double y1, double z1, double x2, double y2, double z2){ double a, b, c, d = 0; a = pow(x1 - x2,2); b = pow(y1 - y2, 2); c = pow(z1 - z2, 2); d = sqrt(a + b + c); return(d); } double particleGravity(double mass1, double mass2, double distance){ double m, f = 0.00000000006; m = mass1 * mass2; f = GC * (m/pow(distance, 2)); return (f); } void tempDistance(void){ int i, m; double d, g; for(i = 0; i<NUM_PARTICLES; i++){ for(m=0;m<(NUM_PARTICLES-1);m++){ d = distance(Points[i][0], Points[i][1], Points[i][2], Points[m][0], Points[m][1], Points[m][2]); distances[i][m] = d; g = particleGravity(mass[i], mass[m], distances[i][m]); pGrav[i][m] = g; } } } //acceleration is dv/dt Calculation, dv/dt = F/m, Right now this is using the constant for the Earth's gravity double accel (double mass, double force){ mass = 1.0; return force/mass; } //gravity between particles calculation void initialPoints(void){ int i; for(i=0;i<NUM_PARTICLES;i++) { //initial data for list of particles Points[i][0] = radius[i]; Points[i][1] = 0; Points[i][2] = Elevation(orbitalAngles[i], radius[i]); finalVelocities[i][0]=20.0; finalVelocities[i][1]=10.0; finalVelocities[i][2]=0.0; } time = 0.0; } int main(){ int i, m; initialPoints(); tempDistance(); //a study of earth for(m = 0;m<1;m++){ printf("%s.mass = %f\n",names[m], mass[m]); printf("%s.orbitalRadius = %f\n",names[m], radius[m]); printf("%s.orbitalAngle = %f\n", names[m], orbitalAngles[m]); printf("%s.elevation = %f AU\n",names[m], (Points[m][2]/AU)); printf("%s.position = %f AU %f AU %f AU\n",names[m], Points[m][0]/AU, Points[m][1]/AU, Points[m][2]/AU); for(i=0;i<NUM_PARTICLES-1;i++){ printf("%s.distanceFrom%d = %f AU\n",names[m], i,distances[m][i]/AU); } for(i=0;i<NUM_PARTICLES;i++){ if(i==3) i = i+1; printf("%s.gravityFrom%d = %f N\n",names[m],i,pGrav[m][i]); } } return(0); }

Share this post


Link to post
Share on other sites
Quote:
Unhandled exception at 0x6c18320f in CoordinatesTest.exe: 0xC0000005: Access violation reading location 0x00000053.

Translation:
Quote:
The code at address 0x6c18320f, in CoordinatesTest.exe tried to read from address 0x00000053, which is not a valid address

Which looks to me like you're dereferencing a null pointer, and reading a variable 0x53 bytes from the start of the allocation (Either a class or an array).

Using the debugger, what line does your code crash on?

Share this post


Link to post
Share on other sites
Follow Evil Steve's advice...

...and I'll throw in that "0x00000053" is 83 in decimal. Are you setting anything to "83" and then trying to use it as a pointer?

Share this post


Link to post
Share on other sites
The problem is here:
    printf("%s.mass = %f\n",names[m], mass[m]);
The %s format needs a char * parameter. names[m] is a char, not a char *.

Change this line:
    char names[NUM_PARTICLES] = {'S', 'M', 'V', 'E', 'm', 'J', 'S', 'U', 'N'}; 
to this and that problem should be fixed:
    char * names[NUM_PARTICLES] = {"S", "M", "V", "E", "m", "J", "S", "U", "N"}; 

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Unhandled exception at 0x6c18320f in CoordinatesTest.exe: 0xC0000005: Access violation reading location 0x00000053.

Translation:
Quote:
The code at address 0x6c18320f, in CoordinatesTest.exe tried to read from address 0x00000053, which is not a valid address

Which looks to me like you're dereferencing a null pointer, and reading a variable 0x53 bytes from the start of the allocation (Either a class or an array).

Using the debugger, what line does your code crash on?


OK, so first I used JohnBolton's advice and changed the declaration of the char array, which worked on this simpler version of the code, and actually got it running correctly for the first time, so thank you very much for that. But I also have a more complicated version that basically takes the values calculated in the previous code and turns them into a simulation. Its a work in progress, but should be able to at least display the initial position and state of each particle, or planet, as this is a solar system simulator as a precursor to a final project. Unfortunately, when i changed the char array declaration and initialization in this more advanced version, it continued to give me the same error :

Unhandled exception at 0x6a41b0b4 in ZiembaCode3.2.exe: 0xC0000005: Access violation reading location 0x00000000.

I've added the later code, any reason why it would be doing this?

By the way, when I debug, the program crashes on a line in glut.h, as im using OpenGL for the graphics part of this problem, that creates the window:

static int APIENTRY glutCreateWindow_ATEXIT_HACK(const char *title) { return __glutCreateWindowWithExit(title, exit); }

Here's the code:

/*this latest version creates a horizontal line of 24 GL_POINTS along the x-axis, with a vector of

(1.0, 0.0, 0.0) and acceleration constant of (0.0, -9.8, 0.0). Hopefully this accurately depicts gravity

but let me know what has to be changed if not.*/

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include "GL/glext.h"
#include "GL/glut.h"

#define NUM_PARTICLES 9
#define EARTH_MASS 5.9742E+24
#define AU 149598000//kilometers
#define GC 6.0E-14//pow(kilometer,3)/kg * pow(s, 2)
#define PI 3.14159265358979323846264338327950

char *names[NUM_PARTICLES] = {"S", "M", "V", "E", "m", "J", "S", "U", "N"};
double Points[NUM_PARTICLES][3];
double Velocities[NUM_PARTICLES][NUM_PARTICLES-1][3];
double finalVelocities[NUM_PARTICLES][3];
double force[3]={0.0, -9.8, 0.0};// force due to Earth's gravity
double distances[NUM_PARTICLES][NUM_PARTICLES-1];//array of the distances between particles, first element is between first and second particle etc.
double pGrav[NUM_PARTICLES][NUM_PARTICLES-1];//array of the gravity force between particles using distances[] to calculate it, and storing in the same manner
double radius[NUM_PARTICLES] = {0.00 * AU, 0.39 * AU, 0.72 * AU, 1.00 * AU, 1.52 * AU, 5.20 * AU, 9.54 * AU, 19.22 * AU,
30.06 * AU};
double orbitalAngles[NUM_PARTICLES] = {0.00, 3.38, 3.86, 7.25, 5.65, 6.09, 5.51, 6.48, 6.43};
double elevation[NUM_PARTICLES];
double mass[NUM_PARTICLES] = {3329146 * EARTH_MASS,0.055 *EARTH_MASS, 0.815 * EARTH_MASS, 1.0 * EARTH_MASS, 0.107 * EARTH_MASS, 317.8 *EARTH_MASS, 95.152 * EARTH_MASS,
14.536 * EARTH_MASS, 17.147 * EARTH_MASS};//mass of the planets as multiples if Earth
double time_step = 0.01;
double time = 0.0;
int x;



void init (void){

glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);

}

double Elevation(double angle, double radius){
return (sin(angle*(PI/180)) * radius);
}

//3d distance formula
double distance(double x1, double y1, double z1, double x2, double y2, double z2){
double a, b, c, d;
a = pow(x1 - x2,2);
b = pow(y1 - y2, 2);
c = pow(z1 - z2, 2);
d = sqrt(a + b + c);
return(d);
}
//acceleration is dv/dt Calculation, dv/dt = F/m, Right now this is using the constant for the Earth's gravity

double accel (double mass, double force){
mass = 1.0;
return force/mass;
}

//gravity between particles calculation
double particleGravity(double mass1, double mass2, double distance){
double m, f = 0.00000000006;
m = mass1 * mass2;
f = GC * (m/pow(distance, 2));
return (f);
}

// use Euler to find new y(t+1) = y(t) + time_step * slope(t)

double Euler( double y, double slope, double delta){

return y + slope * delta;
}
// solve for velocity and position

void RungaKutta(double time, double time_step)
{
double xscale = 1; //ignore this for now
int n, i, m;
double mass=1;
double half_step = 0.5 * time_step;
double half_time = time + half_step;
double full_time = time + time_step;

double k1, k2, k3, k4;
double ks1, ks2, ks3, ks4;
double v1, v2, v3, v4;
double p1, p2, p3, p4;



for (n=0; n < NUM_PARTICLES; n++){
for(m=0;m<NUM_PARTICLES-1;m++){
for (i=0; i < 3; i++) {

v1 = Velocities[n][m][i];
p1 = Points[n][i];
k1 = accel(mass, pGrav[i][m]);
ks1 = v1*xscale;

v2 = Euler(v1, k1, half_step);
p2 = Euler(p1, ks1, half_step);
k2 = accel(mass, pGrav[i][m]);
ks2 = v2*xscale;

v3 = Euler(v2, k2, half_step);
p3 = Euler(p2, ks2, half_step);
k3 = accel(mass, pGrav[i][m]);
ks3 = v3*xscale;

v4 = Euler(v3, k3, time_step);
p4 = Euler(p3, ks3, time_step);
k4 = accel(mass, pGrav[i][m]);
ks4 = v4*xscale;

//n represents wat particle the gravity is acting upon, m represents the particles that the gravity is coming from, and i is the direction of the force of gravity
Velocities[n][m][i] = v1 + (time_step / 6.0) * (k1 + 2.0 * k2 + 2.0 * k3 + k4);
// Points[n][i] = p1 + (time_step / 6.0) * (ks1 + 2.0 * ks2 + 2.0 * ks3 + ks4);
}

}

}
}



void initialPoints(void){

int i;
for(i=0;i<NUM_PARTICLES;i++)
{
//initial data for list of particles
Points[i][0] = radius[i];
Points[i][1] = 0;
Points[i][2] = Elevation(orbitalAngles[i], radius[i]);
finalVelocities[i][0]=20.0;
finalVelocities[i][1]=10.0;
finalVelocities[i][2]=0.0;

}

time = 0.0;

}

void display(void){
double d;
double g;
int m, i, c;
glEnable(GL_POINT_SMOOTH);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

if(time<10)
{
glBegin(GL_POINTS);
for(m=0;m < NUM_PARTICLES; m++)

{
//Display Particles
glVertex3f(Points[m][0], Points[m][1], Points[m][2]);
}
glEnd();
glPopMatrix();
}

glutSwapBuffers();

time=time+time_step;
if(time < 20.5 && time > 20){
for(c = 0;c<NUM_PARTICLES-1;c++){
printf("Distance btwn p%d and p%d = %f\n",c+1, c+2, distances[c]);
printf("Gravity btwn p%d and p%d = %f\n",c+1, c+2, pGrav[c]);
}
}

//Push Particles using RK//
RungaKutta (time, time_step);

for(i = 0; i<NUM_PARTICLES; i++){
for(m=0;m<NUM_PARTICLES-1;i++){
d = distance(Points[i][0], Points[i][1], Points[i][2], Points[m][0], Points[m][1], Points[m][2]);
distances[i][m] = d;
g = particleGravity(mass[i], mass[m], distances[i][m]);
pGrav[i][m] = g;
}
}
}
void tempDistance(void){
int i, m;
double d, g;
for(i = 0; i<NUM_PARTICLES; i++){
for(m=0;m<(NUM_PARTICLES-1);m++){
d = distance(Points[i][0], Points[i][1], Points[i][2], Points[m][0], Points[m][1], Points[m][2]);
distances[i][m] = d;
g = particleGravity(mass[i], mass[m], distances[i][m]);
pGrav[i][m] = g;
}
}
}
// this keeps the simulation running

void idle(void){
glutPostRedisplay();
}

//keyboard function (escape ends program, space begins simulation)
void keyboard(unsigned char key, int x, int y){

switch(key){
case ' ':
initialPoints();
break;

case 27:
exit(0);
break;

}

}


void reshape (int w, int h){

//all the same as from the first program
glViewport(0.0,0.0,(GLdouble) w, (GLdouble) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-30.5 * AU, 30.5 * AU, -30.5 * AU, 30.5 *AU, 2.2*AU, -2.2*AU);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

int main(int argc, char *argv[]){
int i, m;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512, 512);
glutInitWindowPosition(0,0);
glutCreateWindow("Sh Sample Code");
glEnable(GL_POINT_SMOOTH);
init();
initialPoints();
for(i = 0;i<NUM_PARTICLES;i++){
printf("Elevation of particle %d is: %f AU\n",i, (Points[i][2]/AU));
}
glutIdleFunc(idle);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
initialPoints();
tempDistance();
//a study of earth
for(m = 0;m<NUM_PARTICLES;m++){
printf("%s.mass = %f\n",names[m], mass[m]);
printf("%s.orbitalRadius = %f\n",names[m], radius[m]);
printf("%s.orbitalAngle = %f\n", names[m], orbitalAngles[m]);
printf("%s.elevation = %f AU\n",names[m], (Points[m][2]/AU));
printf("%s.position = %f AU %f AU %f AU\n",names[m], Points[m][0]/AU, Points[m][1]/AU, Points[m][2]/AU);
for(i=0;i<NUM_PARTICLES-1;i++){
printf("%s.distanceFrom%d = %f AU\n",names[m], i,distances[m][i]/AU);
}
for(i=0;i<NUM_PARTICLES;i++){
if(i==3)
i = i+1;
printf("%s.gravityFrom%d = %f N\n",names[m],i,pGrav[m][i]);
}
}
return(0);

}

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
The problem is here:
    printf("%s.mass = %f\n",names[m], mass[m]);
The %s format needs a char * parameter. names[m] is a char, not a char *.

Change this line:
    char names[NUM_PARTICLES] = {'S', 'M', 'V', 'E', 'm', 'J', 'S', 'U', 'N'}; 
to this and that problem should be fixed:
    char * names[NUM_PARTICLES] = {"S", "M", "V", "E", "m", "J", "S", "U", "N"}; 


... and ASCII 'S' happens to be decimal 83... i'nt that special! [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by noviceprogmr84
Unfortunately, when i changed the char array declaration and initialization in this more advanced version, it continued to give me the same error :

Unhandled exception at 0x6a41b0b4 in ZiembaCode3.2.exe: 0xC0000005: Access violation reading location 0x00000000.

I've added the later code, any reason why it would be doing this?


As stated before, this error message usually means that some pointer in your code is being dereferenced and it has the value of 0, which of course is invalid. What you need to do is to

learn to use the debugger

Until you learn to use the debugger, you will be crippled because much of your time for the next 50 years will be spent debugging your code. Dereferencing a NULL pointer (the problem in this case) is a very common bug. Are you going to have to post your code every time you get this error? Learn to use the debugger so you can step through your code and find out which pointer is being dereferenced and how it is getting set to 0. Then, you can fix the problem yourself.

Share this post


Link to post
Share on other sites

This topic is 3787 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this