begin process at 2012 05 29 08:28:54
  Trouver un code source :
 
dans
 
Accueil > Forum > 

C

 > 

Linux

 > 

Autre

 > 

erreur de mémoire mais je trouve pas le bug


Derniers messages déposésPoser une question dans le forum ou lancer une discussion

erreur de mémoire mais je trouve pas le bug

mercredi 16 mai 2007 à 20:49:15 | erreur de mémoire mais je trouve pas le bug

dybman

Voici le code qui pose problème:

Je pense que la valeur Key prend de trop grande valeur et fais un dépassement de mémoire tampon mais je trouve pas le bug.

Merci pour votre aide.

/* Hash.C */

#include <stdlib.h>

#include "hash.h"
#include "types.h"
#include "mboard.h"

/* Macros */

#define BCH(N)          (BchTable[(uint8)(N)])
#define CODE(A,B,C,D,N) ((((((BCH((A)*(N))<<8)|BCH((B)*(N)))<<8)|BCH((C)*(N)))<<8)|BCH((D)*(N)))

/* Types */

typedef struct {
   uint32 Lock;
   info   Info;
} entry;

/* Variables */

int HashDate, HashScheme;

int ReadHashTotal, ReadHashTrue;
int WriteHashTotal, WriteHashTrue;

uint64 HashKeyTable[SQUARE_NB][PIECE_NB];

int HashAge[4];

static int    HashBitNb, DateBitNb, DateSize, ClusterSize;
static uint32 HashSize;
static entry *HashTable;

static uint8  BchTable[256];
static uint8  BchSeed;

/* Prototypes */

static void   SetDate    (int Date);
static int    EntryAge   (int Date);

static void   InitRandom (void);
static uint64 Rand64     (void);

/* Functions */

/* InitHash() */

void InitHash(void) {

   int S, P;

   HashBitNb = 20;
   HashSize  = 1 << HashBitNb;

   ClusterSize = 4;

   HashTable = malloc((HashSize+ClusterSize-1)*sizeof(entry));
   assert(HashTable!=NULL);

   DateBitNb = 2;
   DateSize  = 1 << DateBitNb;

   SetDate(0);
   ClearHashTable();

   InitRandom();

   for (S = 0; S <= 50; S++) { /* 0 maps to 0 (side to move) */
      for (P = 0; P < PIECE_NB; P++) {
         HashKeyTable[Std2Sq[S]][P] = Rand64();
      }
   }
}

/* ClearHashTable() */

void ClearHashTable(void) {

   int I,N;
   entry *Entry;
   for (I = 0; I < HashSize+ClusterSize-1; I++) {
      Entry = &HashTable[I];
      Entry->Lock = 0x00000000;
      ClearInfo(&Entry->Info);
   }
}

/* ClearInfo() */

void ClearInfo(info *Info) {

   Info->Date  = HashDate;
   Info->Depth = 0;
   Info->Min   = FALSE;
   Info->Max   = FALSE;
   Info->Value = 0;
   Info->Start = 0;
   Info->End   = 0;
}

/* UpdateHash() */

void UpdateHash(void) {

   SetDate((HashDate+1)%DateSize);
}

/* SetDate() */

static void SetDate(int Date) {

   int D;

   assert(Date>=0&&Date<DateSize);

   HashDate = Date;

   for (D = 0; D < DateSize; D++) HashAge[D] = EntryAge(D);
}

/* EntryAge() */

static int EntryAge(int Date) {

   int Age;

   assert(Date>=0&&Date<DateSize);

   Age = HashDate - Date;
   if (Age < 0) Age += DateSize;

   return Age;
}

/* ReadHash() */

info *ReadHash(uint64 Key, int Depth) {

   uint32 Key0, Key1;
   entry *Entry, *End;
   info *Info;

   ReadHashTotal++;

   Key0 = Key >> 32;
   Key1 = (uint32) Key;

   Entry = &HashTable[Key0];
   End   = Entry + ClusterSize;

   do {
      if (Entry->Lock == Key1) {
         ReadHashTrue++;
         Info = &Entry->Info;
         Info->Date = HashDate;
         return Info;
      }
   } while (++Entry < End);

   return NULL;
}

/* WriteHash() */

info *WriteHash(uint64 Key, int Depth) {

   int Diff, Age, BestAge, BestDepth;
   uint32 Key0, Key1;
   entry *Entry, *End, *Best;
   info *Info;

   WriteHashTotal++;

   Key0 = Key >> 32;
   Key1 = (uint32) Key;

   Entry = &HashTable[Key0];
   End   = Entry + ClusterSize;

   BestAge = -1;

   do {

      if (Entry->Lock == Key1) {
         Info = &Entry->Info;
         Info->Date = HashDate;
         if (Info->Depth <= Depth) {
            WriteHashTrue++;
            return Info;
         }
         return NULL;
      }

      Age  = HashAge[Entry->Info.Date];
      Diff = BestAge - Age;
      if (Diff < 0 || (Diff == 0 && BestDepth > Entry->Info.Depth)) {
         Best      = Entry;
         BestAge   = Age;
         BestDepth = Best->Info.Depth;
      }

   } while (++Entry < End);

   WriteHashTrue++;

   Best->Lock = Key1;
   Info = &Best->Info;
   ClearInfo(Info);

   return Info;
}

/* HashKey() */

uint64 HashKey(const mboard *Board) {

   int S, P;
   uint64 Key;

   Key = 0ULL;

   S = 0; /* Turn */
   P = (Board->Colour == WHITE) ? WHITE_MAN : BLACK_MAN;
   Key ^= HashKeyTable[S][P];

   for (S = S1; S <= S50; S++) {
      P = HashPiece(&Board->Square[S]);
      if (P != NO_PIECE) Key ^= HashKeyTable[S][P];
   }

   return Key;
}

/* HashPiece() */

int HashPiece(const msquare *Square) {

   int P;

   P = NO_PIECE;

   switch (Square->Colour) {
   case WHITE :
      P = (! Square->IsKing) ? WHITE_MAN : WHITE_KING;
      break;
   case BLACK :
      P = (! Square->IsKing) ? BLACK_MAN : BLACK_KING;
      break;
   }

   return P;
}

/* InitRandom() */

static void InitRandom(void) {

   int I, N;

   for (I = 0, N = 0x01; I < 256; I++) {
      BchTable[I] = N;
      if ((N += N) >= 0x100) N ^= 0x171;
   }
   BchTable[255] = 0; /* So that every value is used only once */

   BchSeed = 0;
}

/* Random64() */

static uint64 Rand64(void) {

   uint32 Key, Lock;

   BchSeed++; /* To avoid the 0 value */

   Key  = CODE(7,5,3,1,BchSeed) & (HashSize - 1);
   Lock = CODE(15,13,11,9,BchSeed);

   return (((uint64) Key) << 32) | (uint64) Lock;
}

/* End of Hash.C */
jeudi 17 mai 2007 à 12:35:23 | Re : erreur de mémoire mais je trouve pas le bug

luhtor

Bas a toi de regarder, mais des tests dans ton code la ou tu pense qu'il peut y avoir un pb, et fait des sorties sur la console pour savoir ou ya des pbs. On est pas mieux placé que toi pour débugger ton code. Tu peux utiliser des macros pour ca:

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

#define ASSERT(X) \
{ \
    if (!(X))\
    { \
        printf("Condition non verifiee (%s)  <%s> <ligne %d>\n", #X, __FILE__, __LINE__);\
        system("PAUSE");\
        exit(1);\
    } \
}

int main()
{
    ASSERT(5 < 4); // Evidemment, le test échoue. Donc un msg est affiché.

    printf("coucou"); // ligne non affichée
   
    system("PAUSE");
   
    return 0;
};


Donc tu places en haut de ton fichier, cette macro ASSERT(X) et tu fais des tests partout.
Quand ton programme marche, tu neutralises l'effet de la macro, on changeant sa définition:
#define ASSERT(X)

La macro sera donc vide, et aucun test ne sera généré.
jeudi 17 mai 2007 à 15:59:05 | Re : erreur de mémoire mais je trouve pas le bug

dybman

voici l'adresse du site si quelqu'un souhaite m'aider:

http://perso.orange.fr/eddy.balavoine/VIRTUAL%20DRAUGHTS/index.htm

merci d'avance


Cette discussion est classée dans : int, key, void, info, entry


Répondre à ce message

Sujets en rapport avec ce message

pb de Z-buffer ac openGL -> Help! [ par Arnaud16022 ] bonjour tt le monde!quelqun pourrait me dire pourqoui le Z-buffer marche pas?pasque le dernier (4ème) triangle dessiné apparait tjs au dessus, meme s' Fch. Header :: CONIO.H [ par TontOnDuWeb ] Pour ce que ca interesse (avec vc++ les fonctions suivantes e sont pas incluse (du moins je crois...))>>#if !defined(__CONIO_H)#define __CONIO_H#if !d class.... [ par Tautau ] voila j'ai un petit prob lors de ma compilation et j'ai un test dessus lundi :#include "conio.h"#include "iostream.h"class C_Tableau{ private: équation et tableaux [ par cabarrus ] je ne trouve pas l'erreur dans mon programme?#include#includeint deltanul(int);float deltainf(float);float deltasup(float);void main(void){float a,b,c une fiche de renseignement [ par cabarrus ] je cherche à faire un programme qui demande des renseignements pour pouvoir ensuite les affiché comme une fiche d'identité!!!voici monprogramme mais m Probleme fonctions [niveau debutant] [ par zzzzzz ] en fait je voulais faire une applic qui nous demande un nombre de part et de fin si on met par exemple 2 et 7 sa ecrira 234567 grace a une boucle. le getch ou getchar() ? [niveau debutant] [ par zzzzzz ] :P //---------------------------------------------------------------------------#include #include // getch()#include // c Snake tsssssssssss aidez moiiiiiiii [ par AmK ] Salut ,Je suis en train de coder un snake et la je crois avoir bien compris le principe de l'algo mais niveau code ça foire je sais pas pourquoi voila void et int [ par xionoxid ] SalutC koi la difference entre unvoid a;et int a; ?? J'ai passé ma journée dessus, je vais peter un cable [ par fred23 ] Je suis crevé et dégouté. Je viens de passer ma journée sur ces codes de M.... que j'ai extrais de source trouvées sur le site.En tout et pour tout, j


Nos sponsors


Sondage...

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

Photothèque

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,749 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales