C言語関係掲示板

過去ログ

No855 アクセス・バイオレーション

[戻る] [ホームページ]
No.10993

構造体のプログラムについて
投稿者---atomicchildren(2003/12/10 16:12:18)


このプログラムはBorland C++ Compiler 5.5だと動くのですが
Microsoft Visual C++6.0だと動きません。
どこがいけないのか教えてください。


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

const int N=14;
const int M=30;

typedef struct PARTICLE{
    double data_x;
    double data_y;
    double v_x;
    double v_y;
    double mass;
    int position;
    struct PARTICLE *next;
    struct PARTICLE *subnext;
}Particle;

typedef struct NodeData{
    struct PARTICLE *pfirst;
    double xc;
    double yc;
    double mass;
    int position;
    struct NodeData *child[4];
    int nparticle;
    double length;
    int k;
}Node;

double **make_matrix(int N,int M)
{
    int i;
    double **matrix;
    
    matrix=(double **)calloc(N,sizeof(double*));
    
    if(matrix==NULL){
        printf("memory is now using");
        exit(EXIT_FAILURE);
    }
    
    for(i=0;i<N;i++){
        matrix[i]=(double *)calloc(M,sizeof(double));
        if(matrix[i]==NULL){
            printf("memory is now using");
            exit(1);
        }
    }
    return matrix;
}

Node *make_structmatrix(int N)
{
    Node *matrix;
    
    matrix=(Node *)malloc(sizeof(Node)*N);
    
    if(matrix==NULL)
    {
        printf("memory is now using");
        exit(1);
    }
    return matrix;
}


Particle *make_struct(int N)
{
    Particle *matrix;
    
    matrix=(Particle *)malloc(sizeof(Particle)*N);
    
    if(matrix==NULL)
    {
        printf("memory is now using");
        exit(1);
    }
    return matrix;
}

void make_list(Particle *p,double **dat,Node *root)
{
    int i;
    
    root->xc=8.0;
    root->yc=8.0;
    p->data_x=dat[0][0];
    p->data_y=dat[0][1];
    p->v_x=0;
    p->v_x=0;
    root->pfirst=p;
    for(i=1;i<N;i++){
        (p+i)->data_x=dat[i][0];
        (p+i)->data_y=dat[i][1];
        (p+i)->v_x=0.0;
        (p+i)->v_y=0.0;
        (p+i)->mass=1.0;
        (p+i)->position=0;
        (p+i-1)->next=(p+i);
    }
    (p+N)->next=NULL;
}




main()
{
    int i,k;
    double **dat;
    double x,y;
    int remain,top;
    double a[8]={-1.0,-1.0,1.0,-1.0,-1.0,1.0,1.0,1.0};
    Particle *p,*the;
    Node *b,*root;
    FILE *fp;
    
    remain=M-1;
    top=1;
    
    
    dat=make_matrix(N,4);
    b=make_structmatrix(M);
    p=make_struct(N);
    
    fp=fopen("Nobody.txt","r");
    if(fp==NULL)
    {
        printf("Don't exist 'Nobody.txt\n'");
        exit(0);
    }
    k=0;
    while(fscanf(fp,"%lf %lf",&x,&y)!=EOF)
    {
        dat[k][0]=x;
        dat[k][1]=y;
        dat[k][2]=0.0;
        dat[k][3]=0.0;
        k++;
    }
    root=b;
    make_list(p,dat,root);
    
    for(the=root->pfirst;the!=NULL;the=the->next)
    {
        printf("%lf %lf\n",the->data_x,the->data_y);
    }
    free(dat);
    free(b);
    free(p);
    fclose(fp);
    return 0;
}


No.10997

Re:構造体のプログラムについて
投稿者---たか(2003/12/10 16:48:56)


>このプログラムはBorland C++ Compiler 5.5だと動くのですが
>Microsoft Visual C++6.0だと動きません。
>どこがいけないのか教えてください。

実際に動かして検証してみたいので、Nobody.txtも例を挙げて下さいま
せんか?

No.11005

Re:構造体のプログラムについて
投稿者---atomicchildren(2003/12/10 17:39:26)


早いレスありがとうございます。
Nobody.txtに関しては下の数字です。
すみません忘れておりました。

10.5 10.4
2.4 4.5
3.8 9.3
3.5 5.6
1.5 5.3
7.8 9.9
12.4 11.4
12.43 6.9
12.1 9.1
9.8 7.4
4.5 6.7
13.2 1.1
0.4 3.4
3.1 0.1


No.11007

Re:構造体のプログラムについて
投稿者---たか(2003/12/10 18:21:57)


Borland-C++ 5.6.4でもCode Guardを効かせてコンパイルすると実行時
エラーが出ます。

理由1:
void make_list(Particle *p, double **dat, Node *root)内

(p + N)->next = NULL; ですが、pはmake_struct内でN個作られていま
す。p + N はN + 1個目の確保されてないオブジェクトを指します。

対策1:
(p + N - 1)->next = NULLとする。

理由2:
double **make_matrix(int N, int M)内

calloc()で確保したメモリブロックが開放されていません。free(dat)だ
けではだめで、dat[i](i=0..N-1)についてもfree()を行い、その後にdat
をfree()する必要があります。

対策2:
for (i = 0; i < N; i++) free(dat[i]);
free(dat);

なおVC++で最初にエラーが出たのは、malloc()やcalloc()の確保の仕方が
Borland-C++より厳しいのでアクセス・バイオレーションが出たのだと思い
ます。

No.11008

Re:構造体のプログラムについて
投稿者---atomicchildren(2003/12/10 18:30:35)


ありがとうございました。
直してみます。御指摘ありがとうございました。