簡體   English   中英

C 項目在 Linux 中也能正常工作,但在 MacO 中不能工作

[英]C projet work as well in Linux but not working in MacOs

我的朋友在他的 Linux 計算機上構建了一個 C 項目,當他將其發送給我時,我試圖執行它,但它沒有工作。 你能幫我找到解決方案嗎?
我的 MacO 中的錯誤消息

當我嘗試調試時,我在這里發現了一個問題,它卡在了這個 while 循環中在此處輸入圖像描述

我們主要調用 function init_dicContexte()

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

#include "liste_mots.h"
#include "liste_freq.h"
#include "dict_contexte.h"


int main(void) {

    char* texte;

    FILE* fichier;
    if ((fichier = fopen("Jane_Austen_Emma2.txt", "r"))==NULL)
        raler("fopen");

    if(fseek( fichier , 0L , SEEK_END))
        raler("fseek");
    long taille_fichier = ftell(fichier);
    rewind(fichier);

    texte = calloc(1, taille_fichier+1);
    if (texte == NULL){
        fclose(fichier);
        raler("calloc");
    }

    if(fread(texte, taille_fichier, 1, fichier)!=1){
        fclose(fichier);
        free(texte);
        raler("fread");
    }

    dictContexte mon_dict = init_dictContexte(texte);

    afficher_dict(mon_dict);

    genererTexte(mon_dict, 15);

    free(texte);
    return 0;
}

init_dictContext() 就是這樣

dictContexte init_dictContexte(char* t) {

    int i = 0;

    listeFreq liste_freq = frequencesDe(t);
    _listeFreq freq_cour = liste_freq.liste;

    // allocation de l'espace mémoire
    dictContexte nouv_dict = malloc(sizeof(struct a_dictContexte));
    dictContexte contexte_cour = nouv_dict;

    printf("Creation du dictionnaire de contexte..\n");

    while (freq_cour->freq_suivante!=NULL && k <2) {

        i++;

        contexte_cour->contexte = contexte(t, freq_cour->mot);
        strcpy(contexte_cour->mot, freq_cour->mot);

        dictContexte nouv_contexte = malloc(sizeof(struct a_dictContexte));
        strcpy(nouv_contexte->mot, "\0");
        contexte_cour->suivant = nouv_contexte;

        // ajout d'un contexte
        contexte_cour = nouv_contexte;
        freq_cour = freq_cour->freq_suivante;
    }

    nouv_dict->taille = i;

    detruit_liste_freq(liste_freq.liste);
    return nouv_dict;
}

看起來您正在嘗試創建一個鏈接列表。 通常,列表中最后一個節點的“下一個”成員應設置為NULL ,以便您知道列表的結束位置。 我看不出你正在這樣做。

有時可能會發生 malloc 返回的malloc被初始化為零的情況。 發生這種情況時,“下一個”成員將具有值NULL並且程序將具有工作的外觀。 但這純粹是偶然的,你不能依賴它。

錯誤: pointer being free'd was not allocated堆損壞的一個示例。

堆損壞錯誤是臭名昭著的,因為程序似乎可以工作,然后在給定不同的輸入或移動到不同的系統時突然崩潰。

如果您的朋友要使用地址清理程序( gcc -fsanitize=address... )在 Linux 上構建程序,他會發現該錯誤也存在於 ZEDC9F0A5A5D57797BF68E37364748 上。

為了告訴您實際的錯誤在哪里,我們需要一個MCVE

但是您可以自己動手:在調試器下運行可執行文件,在malloc_error_break上設置斷點(如錯誤消息告訴您的那樣),使用where命令,您將准確找出檢測到錯誤的位置。

PS看起來free()的指針尚未初始化(即包含“隨機”垃圾)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM