细分错误和链接列表的未知问题 [英] Unknown Issue with segmentation fault and linked lists

查看:40
本文介绍了细分错误和链接列表的未知问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试编写机械程序.该程序很长,但以下是导致我出现问题的函数: recherche_noe creation_noe .无需打扰其余的人.

So I am trying to program a mechanical program. The program is long but here are the functions that are causing me problems: recherche_noe and creation_noe. No need to bother with the rest.

它是法语的,所以请允许我,但是想法是这样的:首先在 main 中,我问用户 lst_noe noe 的数量>(这是 noe 的列表).在使用 creation_noe 时,他在向用户询问结构信息时做到了这一点.最后, recherche_noe 返回我要查找的 noe .所有信息都存储在 struct maillage 中,您还可以在其中包含其他结构.谢谢您的帮助.

It's in french so bear with me but the idea is this: first in the main I ask the user the number of noe in lst_noe (which is a list of noe). With creation_noe he makes that while asking the user for info for the structure. Finally recherche_noe returns the noe that I am looking for. All the info is stored in struct maillage which you have other structures inside. Thank you for your help.

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



/*==============================================*/
/*     Déclaration des structures    */
/*==============================================*/

struct matrice
{
    char nom[20];
    int n,m;
    double **tab;
    struct  matrice *next;
};

struct element
{
    int num;
    int n1, n2;
    double k;
    struct element *next;
};

struct noeud
{
    int num;
    double u;
    double f;
    struct noeud *next;
};

struct maillage
{
    int nb_noe, nb_elt;
    struct noeud *lst_noe;
    struct element *lst_elt;
    struct matrice *K, *U, *F;
};

typedef struct matrice* matrices;
typedef struct element* elements;
typedef struct noeud* noeuds;
typedef struct maillage* maillages;
char buffer[100];


/*==============================================*/
/*               Recherche       */
/*==============================================*/

noeuds recherche_noe(maillages mail,int num){
    int i;
    maillages temp=mail;

    while(temp->lst_noe!=NULL){

        if(temp->lst_noe->num == num)
            return temp;
        temp->lst_noe=temp->lst_noe->next;
    }
    printf("Le noeud recherche n'existe pas");
    return temp;
}

elements recherche_elt(maillages mail,int num){
    int i;
    maillages temp=mail;

    while(temp->lst_elt->num != num /*&& temp->lst_elt->next!=NULL*/){
        temp->lst_elt=temp->lst_elt->next;
    }
    if(temp->lst_elt->num != num /*&& temp->lst_elt->next==NULL*/){
        printf("L'element recherche n'existe pas");
    }else{
        return mail->lst_elt;
    }
}


/*==============================================*/
/*                   creation                   */
/*==============================================*/



matrices creation_noeud(maillages mail){
    int i;
    for (i=0;i<mail->nb_noe;i++){
        noeuds new = (noeuds)malloc(sizeof(struct noeud));
        new->num = i+1;
        printf("Deplacement du noeud %d: ",i+1);
        buffer[0]='\0';
        getchar();  //reinitialisation de buffer
        scanf("%[^\n]",buffer);
        if((int) strlen(buffer)){    //si la taille du buffer différente 0
            new->u= (double)atof(buffer);
        }
        else{
            printf("Donner l'effort %d du noeuds",i+1);
            scanf("%lf", &new->f);
        }
        new->next=mail->lst_noe;
        mail->lst_noe=new;
    }
}

void creation_element(maillages mail)
{
    int i;
    for (i=0;i<mail->nb_elt;i++){
        elements new= (elements)malloc(sizeof(struct element));
        new->num=i+1;
        printf("Donner le noeud 1 de l'element %d: ",i+1);
        scanf("%d", &new->n1);
        printf("Donner le noeud 2 de l'element %d: ",i+1);
        scanf("%d", &new->n2);
        printf("Donner la raideur de l'element %d: ",i+1);
        scanf("%lf",&new->k);
        new->next= mail->lst_elt;
        mail->lst_elt=new;
    }
}


matrices creation_mat(int n,int m, char *nom){
    int i,j;
    matrices new=(matrices)malloc(sizeof(struct matrice));
    strcpy(new->nom,nom);
    new->n = n;
    new->m = m;
    new->tab = (double**)malloc((n)*sizeof(double*));
    for (i=0; i<n; i++)
        new->tab[i] = (double*)malloc((n)*sizeof(double));
    for (i=0;i<n;i++)      /* mise a zero des composantes */
        for (j=0;j<m;j++)
            new->tab[i][j] =0;

    return new;
}


/*==============================================*/
/*               Assemblage          */
/*==============================================*/


void assemblage(maillages mail){
    int a,b,i,j,k;

    mail->K = creation_mat(mail->nb_noe, mail->nb_noe,"K");
    mail->U = creation_mat(mail->nb_noe, 1,"U");



    for (j=0; j<mail->nb_noe; j++){ //Initialisation de K
            for(k=0; k<mail->nb_noe; k++){
                mail->K->tab[j][k]=0;
            }
    }
    printf("%d",recherche_elt(mail,i+1)->n1);
    for (i=0; i<mail->nb_elt; i++){ // Assemblage matrice K

        a = recherche_elt(mail,i+1)->n1-1;
        b = recherche_elt(mail,i+1)->n2-1;
        mail->K->tab[a][a] +=recherche_elt(mail,i+1)->k;
        mail->K->tab[a][b] -=recherche_elt(mail,i+1)->k;
        mail->K->tab[b][a] -=recherche_elt(mail,i+1)->k;
        mail->K->tab[b][b] +=recherche_elt(mail,i+1)->k;

    }


    for (i=0; i<mail->nb_noe; i++){ // Assemblage matrice U
        mail->U->tab[i][0] = recherche_noe(mail,i+1)->u;
    }
}

/*==============================================*/
/*               Produit                */
/*==============================================*/


matrices produit(matrices mat1,matrices mat2,char *nom){
    int i,j,k;
    matrices prod;
    if(mat1->m!=mat2->n){
        printf("Erreur, les matrices ne sont pas compatibles\n\n");
    }else{
        prod=malloc(sizeof(struct matrice));
        strcpy(prod->nom,nom);
        prod->next=NULL;
        prod->n=mat1->n;
        prod->m=mat2->m;
        prod->tab= (double **)malloc(prod->n * sizeof(double *));
        for (i=0; i<prod->n; i++)
            prod->tab[i] = (double *)malloc(prod->m * sizeof(double));

        for (i=0;i<prod->n;i++){
            for (j=0;j<prod->m;j++){
                prod->tab[i][j]=0;
                for (k=0;k<mat1->m;k++){
                    prod->tab[i][j]+=mat1->tab[i][k] * mat2->tab[k][j];
                }
            }
        }
        return prod;
    }
}

/*==============================================*/
/*               Affichage                */
/*==============================================*/



void affiche_mat(matrices mats){
    int i,j;
    printf("Matrice %s de dimensions %d*%d:\n",mats->nom,mats->n,mats->m);
    for (i=0;i<mats->n;i++){
        for (j=0;j<mats->m;j++){
            printf("%s[%d][%d]: %lf\n",mats->nom,i,j,mats->tab[i][j]);
        }
    }
    printf("\n");
}















int main(){
    int i;
    elements lst_elt;
    noeuds lst_noe;
    maillages mail=malloc(sizeof(struct maillage));
    mail->lst_noe=NULL;
    mail->lst_elt=NULL;


    printf("Donner le nombre de noeuds voulu: ");
    scanf("%d",&mail->nb_noe);
    printf("Donner le nombre d'elements voulu: ");
    scanf("%d",&mail->nb_elt);
    creation_noeud(mail);
    creation_element(mail);


    printf("%d",recherche_elt(mail,2+1)->n1+45);

    assemblage (mail);

    produit(mail->K,mail->U,"F");

    /*affiche_mat(mail->K);
    printf("\n");
    affiche_mat(mail->U);
    printf("\n");
    affiche_mat(mail->F);
    printf("\n");*/





}

推荐答案

在浏览链表时,您正在更改链表的内容,而不是提高温度.

When walking the linked list, you are altering the contents of the linked list, instead of advancing the temp.

elements recherche_elt(maillages mail,int num){
    elements temp;

    for (temp = mail->lst_elt; temp; temp = temp->next ) {
        if (temp->num == num) return temp;
    }
    printf("L'element recherche n'existe pas\n");
    return NULL;
}


然后,在删除typedef后,它变为:


And, after removal of the typedefs, it becomes:

struct element *recherche_elt(struct maillage *mail,int num){
    struct element *this;

    for (this = mail->lst_elt; this; this = this->next ) {
        if (this->num == num) return this;
    }
    printf("L'element recherche n'existe pas\n");
    return NULL;
}


[在代码的其他位置也会重复出现相同类型的错误]


[The same kind of error is reproduced at other places in the code]

这篇关于细分错误和链接列表的未知问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆