Bon après avoir corrigé certaines erreur j'arrive à compresser un petit peu mais ça plante toujours, non pas instantanément mais après un temps de compression.
Donc voici le code de compression:
//Application de l'algorithme
int HUFF2_Compress (H_Tree **PtrRacine, H_ASCII *Table_ASCII, unsigned short &DimAscii, FILE *input, FILE *output)
{
//Variables fichier
unsigned char c; //Caractère
unsigned char byte=0;
unsigned char start=0;
//Table ASCII temporaire de sauvegarde
H_ASCII *table_sav=(H_ASCII *)malloc (sizeof (H_ASCII)*ASCII_MAX);
//Parcours fichier
while (!feof (input))
{
//Caractère en cours
c=fgetc (input);
//Fin du fichier
if (feof (input)) break;
//Ecriture caractère avec le code de Huffman progressif
if (DimAscii>1)
{
//Ecriture code de Huffman
if (HUFF2_FindChar (Table_ASCII, c))
BitWrite (Table_ASCII [c].code, byte, start, Table_ASCII [c].bits, output);
else
BitWrite (c, byte, start, 8, output);
}
//Ecriture simple caractère
else
{
//Simple caractère
BitWrite (c, byte, start, 8, output);
}
//Copie table si au moins 1 caractère
if (DimAscii)
HUFF2_CopyTable (table_sav, Table_ASCII, DimAscii);
//Ajoute fréquence
HUFF2_AddFreq (Table_ASCII, DimAscii, c);
HUFF2_CopyTable (Table_ASCII, table_sav, DimAscii);
//Si plus d'un caractère
if (DimAscii>1)
{
//Création de l'arbre
HUFF_MakeTree (Table_ASCII, DimAscii, PtrRacine);
//Codification
(*PtrRacine)->code=0; (*PtrRacine)->bits=0;
HUFF_CreateCode (Table_ASCII, *PtrRacine);
//Efface ancien arbre
//printf ("%u - %lu\n", DimAscii, *PtrRacine);
//PLANTE à CAUSE DE CETTE LIGNE
*PtrRacine=HUFF2_DeleteTree (*PtrRacine);
}
}
//Octet restant
if (start!=0)
//Ecriture octet
fputc (byte, output);
//Libère mémoire
//HUFF2_DeleteTree (*PtrRacine);
free (table_sav);
}
La fonction qui créer l'arbre:
//Création de l'arbre
int HUFF_MakeTree (H_ASCII *Table_ASCII, unsigned short DimAscii, H_Tree **PtrRacine)
{
H_ASCII tmp_table; //Table temporaire
H_Tree *PtrNoeud;
BOOL insert;
unsigned short index;
unsigned short i, j;
//Parcours fréquences ASCII ordre croissant
for (i=DimAscii-1; i>0; i--)
{
//Allocation pour noeud
PtrNoeud=(H_Tree *)malloc (sizeof (H_Tree));
PtrNoeud->FGauche=(H_Tree *)malloc (sizeof (H_Tree));
PtrNoeud->FDroite=(H_Tree *)malloc (sizeof (H_Tree));
PtrNoeud->FGauche->FGauche=NULL;
PtrNoeud->FGauche->FDroite=NULL;
PtrNoeud->FDroite->FGauche=NULL;
PtrNoeud->FDroite->FDroite=NULL;
PtrNoeud->FGauche->noeud=false;
PtrNoeud->FDroite->noeud=false;
//Erreur out of memory
if (PtrNoeud==NULL) return 0;
//Partie droite (plus petite fréquence)
memcpy (PtrNoeud->FDroite, &Table_ASCII [i].donnee, sizeof (H_Tree));
//Partie gauche (plus grande fréquence)
memcpy (PtrNoeud->FGauche, &Table_ASCII [i-1].donnee, sizeof (H_Tree));
//Cumul des données
//Ajoute les fréquences
tmp_table.freq=Table_ASCII [i].freq+Table_ASCII [i-1].freq;
tmp_table.donnee.noeud=true;
tmp_table.donnee.ptr=PtrNoeud;
//Reclassement du noeud dans la table
for (j=i, insert=false; (j>0) && (!(insert)); j--)
//Redéfinition de la table ASCII (enleve item)
if (Table_ASCII [j-1].freq < tmp_table.freq)
*(Table_ASCII+j)=*(Table_ASCII+j-1);
else {
insert=true;
index=j;}
if (!insert) index=0;
//Combine ASCII
*(Table_ASCII+index)=tmp_table;
}
//Création du pointeur vers la racine de l'arbre
*PtrRacine=PtrNoeud;
//Pas d'erreur
return -1;
}
Et enfin la focntion qui doit théoriquement effacer un arbre.
//Suppression d'un arbre de huffman.
H_Tree *HUFF2_DeleteTree (H_Tree *arbre)
{
if (arbre && arbre->noeud)
{
if (arbre->FGauche && (arbre->FGauche->noeud)) arbre->FGauche=HUFF2_DeleteTree (arbre->FGauche);
if (arbre->FDroite && (arbre->FDroite->noeud)) arbre->FDroite=HUFF2_DeleteTree (arbre->FDroite);
free (arbre);
}
return NULL;
}
Le débogeur ne m'indique plus rien si ce n'est Segmentation de la mémoire maius sans pointer sur une ligne ou afficher une variable. Je ne vois vraiment pas mon ou mes erreurs.