アクセス違反エラー

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
altrouge

アクセス違反エラー

#1

投稿記事 by altrouge » 12年前

お世話になります。以下のプログラムにおいて
「0xC0000005: 場所 0x00000000 を読み込み中にアクセス違反が発生しました」
と43行目でエラーが発生します。
K[j]におけるメモリ確保の仕方に問題があると思うのですが、
自分で調べても解決出来ず、お知恵をお借りできればと思います。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define pi 3.1415926535897931

typedef struct
{
double hx; /* mesh size x */
double hy; /* mesh size y */
int ii; /* number of nodes x */
int jj; /* number of nodes y */
double **p, **f; /* pressure and right hand side value pressure */
double **hfi, **hrhs; /* film thickness and right hand side film thickness */
double **w; /* elastic deformation integrals */
double **K, **K1; /* kernels */
double **pconv; /*converged pressure for convergence check in FMG */
double **pold; /*old pressure for use in FAS coarsening */
double **pjac; /*old pressure for use in jacobi relaxation */
double **A, *X, *Y; /* right hand side of force balance equation */
double rg; /* right hand side of force balance equation */
double Hm, Hcp, Hc; /* minium and central film thickness for output */
} Level;

typedef struct
{
int nx0,ny0; /* number of nodes coarsest grid */
int m1,m2,od; /* number of correction points */
int maxlev, deep; /* number of grid-levels, grids deep */
double xa,xb; /* begin, end of computational domain */
double ya,yb; /* begin, end of computational domain */
double h0,wu; /* global constant and work unit */
Level *Lk; /* array of grid levels */
} Stack;

double MMoes, LMoes, H_0, rlambda, alphabar;
double p0r, alpha, eta0, zr;
int maxl,deepl,starl,order,typecy,ncy,outlev,currfilev;
unsigned long *cputimes,t0;
double hfact,xi_l,urja,urgs;
int typecy;

double **matrix(int nx, int ny, int shift)
{
int i;

double **m=(double **)calloc(nx+1,sizeof(double*));
for (i=0;i<=nx;i++) m=(double *)calloc(ny+1,sizeof(double))+shift;
return m+shift;
}

void initialize(Stack *U, int nx0, int ny0, int maxl, int deepl, int ord,
double xa, double xb, double ya, double yb, double h0)
{
/* initialize values in datastructure */

double hx,hy;
Level *L;
int l,ii,jj;

U->xa=xa;
U->xb=xb;
U->ya=ya;
U->yb=yb;
U->maxlev=maxl;
U->deep=deepl;
U->wu=0.0;
U->od=ord-2;
U->h0= h0;

cputimes=(unsigned long *)calloc(maxl+1,sizeof(unsigned long));

U->Lk=(Level *)calloc(maxl+1,sizeof(Level));

hx=(xb-xa)/nx0;
hy=(yb-ya)/ny0;
ii=nx0;
jj=ny0;
for (l=1;l<=maxl;l++)

{
L=U->Lk+l;
L->hx=hx;
L->hy=hy,
L->ii=ii;
L->jj=jj;
L->p =matrix(ii+2*U->od,jj+2*U->od,U->od);
L->w =matrix(ii+2*U->od,jj+2*U->od,U->od);
L->f =matrix(ii+2*U->od,jj+2*U->od,U->od);
L->pold =matrix(ii+2*U->od,jj+2*U->od,U->od);
L->pjac =matrix(ii+2*U->od,jj+2*U->od,U->od);
L->pconv =matrix(ii+2*U->od,jj+2*U->od,U->od);
L->hfi =matrix(ii+2*U->od,jj+2*U->od,U->od);
L->hrhs =matrix(ii+2*U->od,jj+2*U->od,U->od);

L->K =matrix(ii+4*U->od,jj+4*U->od,0);
L->K1=matrix(ii+4*U->od,jj+4*U->od,0);

L->A=matrix(ii-1,11,0);
L->X=(double *)calloc(ii-1,sizeof(double));
L->Y=(double *)calloc(ii-1,sizeof(double));

printf("\n level: %2d ii=%4d, jj=%4d hx=%f hy=%f",l,ii,jj,hx,hy);

if((2*(l/2)-l)!=0){hx*=0.5; ii*=2;} else {hy*=0.5; jj*=2;}

if ((L->p==NULL)||(L->w==NULL)||(L->f==NULL)||(L->K==NULL)||(L->pold==NULL)
||(L->pconv==NULL)||(L->hfi==NULL)||(L->hrhs==NULL)||(L->K=NULL)
||(L->K1==NULL)||(L->A==NULL)||(L->X==NULL)||(L->Y==NULL))
{
printf("\n problem allocating memory \n");
exit(-1);
}
}
U->m1=(int)(3+log(1.0*(U->Lk+maxl)->ii)); U->m2=2;
printf("\ncorrectin patch in multi-integrationin: m1=%2d, m2=%2d",U->m1,U->m2);
}

void init_log(Stack *U, int l)
{
/* computes the kernel of level l */
int i,j;
Level *L;
double xp,yp,xm,ym;

L =U->Lk+l;

for (i=0;i<=L->ii+4*U->od;i++)
{
xp=(i+0.5)*L->hx; xm=xp-L->hx;
for(j=0;j<=L->jj+4*U->od;j++)
{
yp=(j+0.5)*L->hy; ym=yp-L->hy;
L->K[j]=
fabs(xp)*log(yp/xp+sqrt(1.0+yp*yp/xp/xp))
-fabs(xp)*log(ym/xp+sqrt(1.0+ym*ym/xp/xp))
+fabs(xm)*log(ym/xm+sqrt(1.0+ym*ym/xm/xm))
-fabs(xm)*log(yp/xm+sqrt(1.0+yp*yp/xm/xm))
+fabs(yp)*log(xp/yp+sqrt(1.0+xp*xp/yp/yp))
-fabs(yp)*log(xm/yp+sqrt(1.0+xm*xm/yp/yp))
+fabs(ym)*log(xm/ym+sqrt(1.0+xm*xm/ym/ym))
-fabs(ym)*log(xp/ym+sqrt(1.0+xp*xp/ym/ym)); ←エラー発生個所
}
}
}

void input_loadpar()
/* input of the load conditions for the contact */
{
printf("\ngive M ?") ;scanf("%lf",&MMoes);
printf("\ngive L ?") ;scanf("%lf",&LMoes);

/* conversion to Parameters aPPearing in equations */

rlambda=1.0/(pi*pow(128.0/3.0/pow(MMoes,4.0),1.0/3.0));
alphabar=LMoes/pi*pow(1.5*MMoes,1.0/3.0);

printf("\nrlambda=%8.5e alpha*p_h=%8.5e\n",rlambda,alphabar);

/* computation initial guess H0 */

H_0=1.67*pow(MMoes,-1.0/9.0)-1.897+0.2*LMoes/50;
if (H_0<-0.99) H_0=-0.99;

printf("First approximation HO=%8.5e",H_0);

/* 0ptional use of hand-input HO */
/*
printf("\ngive H0 ?");
scanf("%lf",&H_0);
*/

/* parameter Roelands equation */
p0r=1.96e8;
alpha= 2.2e-8;
eta0=40.0e-3;
zr= (alpha*p0r)/(log(eta0)+9.67);
}

void input_solvepar()
/* input of parameters numerical process */
{
/* account for intermediate grids */
printf("\nhow many levels ?"); scanf("%d",&maxl); maxl=2*maxl-1;
printf("\nStartlevel ?"); scanf("%d",&starl); starl =2*starl-1;

printf("\nhow many cycles ?"); scanf("%d",&ncy);
printf("\ntype of cycle ?"); scanf("%d",&typecy);

deepl=2*maxl-2; /* number of coarse grids in multi-integration */
order=6; /* order of transfer multi-integration */

xi_l=0.3; /* relaxation switch parameter */
urja=0.2; /* underrelaxation jacobi part */
urgs=0.4; /* underrelaxation Gauss- seidel part */

/* factor for relaxation of force balance equation */
if (typecy==2)
hfact=0.05;
else
hfact=0.1;
if((LMoes>10)||(MMoes>1000)) hfact*=0.25;
}


void main()
{
int l;
Stack U;

input_loadpar();
input_solvepar();

initialize(&U,16,16,maxl,deepl,order,-4.5,1.5,-3.0,3.0,H_0);

for (l=1;l<=maxl;l+=2) { init_log(&U,l); }
}

アバター
h2so5
副管理人
記事: 2212
登録日時: 15年前
住所: 東京
連絡を取る:

Re: アクセス違反エラー

#2

投稿記事 by h2so5 » 12年前

コード:

        if ((L->p==NULL)||(L->w==NULL)||(L->f==NULL)||(L->K==NULL)||(L->pold==NULL)
            ||(L->pconv==NULL)||(L->hfi==NULL)||(L->hrhs==NULL)||(L->K=NULL)
            ||(L->K1==NULL)||(L->A==NULL)||(L->X==NULL)||(L->Y==NULL))
        {
            printf("\n problem allocating memory \n");
            exit(-1);
        }
よく見ると L->K=NULL になっています。
オフトピック
Xcodeの静的解析ツールを使ったら検出できました。なかなか優秀ですね。

閉鎖

“C言語何でも質問掲示板” へ戻る