Home > Mobile >  C projet work as well in Linux but not working in MacOs
C projet work as well in Linux but not working in MacOs

Time:12-23

My friend built a C projet on his Linux computer and when he sent it to me and i tried to execute it but it didn't work. Can u help me with finding a solution ?.
enter image description here

in the main we call the 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() is like that

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

CodePudding user response:

The error: pointer being free'd was not allocated is an example of heap corruption.

Heap corruption bugs are notorious in that the program appears to work, and then suddenly crashes when given a different input, or moved to a different system.

If your friend were to build the program on Linux using address sanitizer (gcc -fsanitize=address ...), he would discover that the bug is present on Linux as well.

To tell you where the actual bug is, we'd need an MCVE.

But you can help yourself: run the executable under debugger, set a breakpoint on malloc_error_break (as the error message tells you), use where command and you will find out exactly where the error is detected.

P.S. It looks like the pointer being free() has not been initialized (i.e. contains "random" garbage).

CodePudding user response:

It looks like you are trying to create a linked list. Typically, the "next" member of the last node in the list should be set to NULL so you can know where the list ends. I can't see you are doing that.

Sometimes it may happen that the memory returned by malloc is initialized to zero. When this happen, the "next" member will have the value NULL and the program will have the appearance of working. But this is purely by chance, and you cannot rely on that.

  • Related