【问题标题】:Unknown Issue with segmentation fault and linked lists分段错误和链表的未知问题
【发布时间】:2018-05-20 15:39:46
【问题描述】:

所以我正在尝试编写一个机械程序。 该程序很长,但以下是导致我出现问题的函数:recherche_noecreation_noe。剩下的就不用管了。

它是法语,所以请耐心等待,但想法是这样的:首先在main 中,我询问用户lst_noe 中的noe 的编号(这是noe 的列表)。通过creation_noe,他在向用户询问结构信息的同时做到了这一点。最后recherche_noe 返回我正在寻找的noe。所有信息都存储在struct maillage 中,您在其中还有其他结构。感谢您的帮助。

#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");*/





}

【问题讨论】:

  • 复制/粘贴错误:: new-&gt;tab[i] = (double*)malloc((n)*sizeof(double)); -->> new-&gt;tab[i] = (double*)malloc((m)*sizeof(double)); (或相反)[而且,恕我直言,您应该删除演员表和愚蠢的类型定义]
  • 感谢您发现这个错误。但这并不能解决我的 noe 列表的主要问题。为什么 main 中的 printf 不能正常工作(段错误的来源)。我追溯到 recherche_noe。
  • 另外,您忘记在 product() 中初始化新矩阵并将其归零。而且你的链表代码很麻烦。
  • 矩阵的归零是在创建时完成的,在 creation_mat 中。是的,我很抱歉所有这些基本上都是自学的,老师没用。这就是为什么我仍然对链接列表有一些问题。
  • 是的,你完全正确,我很抱歉。

标签: c linked-list segmentation-fault


【解决方案1】:

当遍历链表时,你是在改变链表的内容,而不是提前。


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 之后,它变成:


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;
}

【代码中其他地方出现了同样的错误】

【讨论】:

  • 是的,我知道问题出在很多地方。我在我的混乱中井井有条。但如果我设法解决了 recherche_noe 问题,我将能够解决其余的问题。只知道这根本不是最终版本,而且我不是程序员,这就是为什么一方面有错误,另一方面我没有正确的反应来获得正确的语法。我是机械工程专业的学生
猜你喜欢
  • 2016-12-11
  • 2017-05-22
  • 1970-01-01
  • 2013-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
相关资源
最近更新 更多