Speicherzugriffsfehler
-
Hallo,
ist es möglich diesen Fehler kurz zu charakterisieren.
besten Dank
Stephan
-
du hast auf speicher zugegriffen der nicht dir gehört
-
bsp: di willst Zeichen eines char* ändern, wenn du
char* x="Hello World";
gemacht hast.
Oder du willst auf einen zeiger zugreifen, der nicht mehr gültig/aktuell/alloziert/NULL ist....
-
leider konne ich die Ursache für den falschen Speicherzugriff finden. Vielleicht kann mir jemand einen Tip geben
besten Dank
Stephan
#include <stdio.h> #include <stdlib.h> #include <math.h> typedef float real; #define sqr(x) ((x)*(x)) const int DIM=1; typedef struct { real m; real x[DIM]; real v[DIM]; real F[DIM]; real F_old[DIM]; } Particle; int N=3; FILE *fz; void force(Particle *i, Particle *j) { real r=0; for (int h=0; h<DIM; h++) r+=sqr(j->x[h] - i->x[h]); real f=i->m * j->m /(sqrt(r) *r); for (int d=0; d<DIM; d++) i->F[d] += f*(j->x[d]-i->x[d]); } void compF_basis(Particle *test, int N){ for (int i=0; i<N; i++) for (int d=0; d<DIM; d++) test[i].F[d]=0; for (int i=0; i<N; i++){ for (int j=0; j<N; j++) if (i!=j) force(&test[i],&test[j]); } } void updateX(Particle *test, real delta_t) { real a=delta_t*.5/test->m; for(int d=0; d<DIM; d++) { test->x[d]+=delta_t*(test->v[d]+a*test->F[d]); test->F_old[d]=test->F[d]; } } void compX_basis(Particle *test, int N, real delta_t) { for(int i=0; i<N; i++) updateX(&test[i],delta_t); } void updateV(Particle *test, real delta_t) { real a=delta_t*.5/test->m; for(int d=0; d<DIM; d++) test->v[d]+=a*(test->F[d]+test->F_old[d]); } void compV_basis(Particle *test, int N, real delta_t) { for(int i=0; i<N; i++) updateV(&test[i],delta_t); } void compoutStatistic_basis(Particle *test, int N, real t) { real e=0; for(int i=0; i<N; i++) { real v=0; for(int d=0; d<DIM; d++) v+=sqr(test[i].v[d]); e+=.5*test[i].m*v; } } void outputResults_basis(Particle *test, int N, real t) { if (N==1) printf(" 1.x 2.x 3.x 4.x 1.v 2.v 3.v 4.v 1.F 2.F 3.F 4.F\n"); for (int i=0; i<N; i++) { for (int h=0; h<DIM; h++){ printf(" %2.2g",test[i].x[h]); fprintf(fz," %2.2g",test[i].x[h]); for (int h=0; h<DIM; h++) fprintf(fz," %2.2g",test[i].F[h]); /*if (h==1) fprintf(fz," \n");*/ } /*for (int h=0; h<DIM; h++){ printf(" %2.2g",test[i].v[h]); fprintf(fz," %2.2g",test[i].v[h]); }*/ if (i==N) printf("\n"); fprintf(fz," \n"); } fprintf(fz," 8\n"); } int main(void) { Particle test[N]; test[0].m=1;test[1].m=3e-6;test[2].m=9.55e-4;test[3].m=1e-14; test[0].x[0]=0;test[1].x[0]=0;test[2].x[0]=0;test[3].x[0]=34.75; test[0].x[1]=0;test[1].x[1]=1;test[2].x[1]=5.36;test[3].x[1]=0; test[0].v[0]=0;test[1].v[0]=-1;test[2].v[0]=-0.425;test[3].v[0]=0; test[0].v[1]=0;test[1].v[1]=0;test[2].v[1]=0;test[3].v[1]=0.0296; real delta_t=0.5; real t_end=6; real t=0; /*outputResults_basis(test, N, t);*/ while (t<t_end){ t+=delta_t; compF_basis(test,N); compX_basis(test, N, delta_t); compV_basis(test, N, delta_t); compoutStatistic_basis(test, N, t); /*outputResults_basis(test, N, t);*/ } return(0); }
-
debugger starten, step-by-step durch den code gehen, fehlerzeile finden, fehler beseitigen, wenn nicht geklappt - dann wieder posten