Etrange étrange

Etrange étrange - C++ - Programmation

Marsh Posté le 12-07-2006 à 12:26:37    

Voici une petite erreur que je viens d'avoir, et dont je voudrais avoir vos avis...
 
Affichage gdb :  


Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x09b608d6
0x022a0134 in Gecode::MiniModel::LinExpr::post (this=0xbfffe41c, home=0x397a0a0, icl=ICL_DEF) at gecode/minimodel/lin-expr.cc:81
81                min += ts[i].a*ts[i].x.min();
(gdb) print i
$3 = 3
(gdb) print n
$4 = 2


 
Code source du fichier lin-expr.cc

Code :
  1. 00073     IntVar
  2. 00074     LinExpr::post(Space* home, IntConLevel icl) const {
  3. 00075       GECODE_AUTOARRAY(Int::Linear::Term, ts, n+1);
  4. 00076       (void) ax->fill(ts,0,sign);
  5. 00077       double min = sign*-c;
  6. 00078       double max = sign*-c;
  7. 00079       for (int i=n; i--; )
  8. 00080         if (ts[i].a > 0) {
  9. 00081           min += ts[i].a*ts[i].x.min();
  10. 00082           max += ts[i].a*ts[i].x.max();
  11. 00083         } else {
  12. 00084           max += ts[i].a*ts[i].x.min();
  13. 00085           min += ts[i].a*ts[i].x.max();
  14. 00086         }
  15. 00087       if (min < Limits::Int::int_min)
  16. 00088         min = Limits::Int::int_min;
  17. 00089       if (max > Limits::Int::int_max)
  18. 00090         max = Limits::Int::int_max;
  19. 00091       IntVar x(home, static_cast<int>(min), static_cast<int>(max));
  20. 00092       ts[n].x = x;
  21. 00093       ts[n].a = -1;
  22. 00094       Int::Linear::post(home, ts, n+1, IRT_EQ, sign*-c, icl);
  23. 00095       return x;
  24. 00096     }


 
Petites explications :
Le code source émane d'une librairie C++ (Gecode)
Le code source mis ci-dessus est bien celui du bon fichier (etc )
 
Ce qui est étrange c'est qu'au sein de la boucle "for" (lignes 79 à 86), la variable "i" n'est jamais incrémentée, pourtant gdb indique que sa valeur (=3) est supérieure à sa valeur d'initialisation (=2)...
 
Y comprenez vous quelque chose ?

Reply

Marsh Posté le 12-07-2006 à 12:26:37   

Reply

Marsh Posté le 12-07-2006 à 12:42:27    

pour t'assurer de l'intégrité de i, dans la boucle
const int c_i = i;
et utilise c_i a la place de i

Reply

Marsh Posté le 12-07-2006 à 12:45:56    

Le soucis est que le code provient d'une bibliothèque, et je n'ai pas très envie de la modifier ...

Reply

Marsh Posté le 12-07-2006 à 12:56:56    

Bon finalement je l'ai fait ...
Revoici donc le code source (juste la boucle for)

Code :
  1. 79 for (int i=n; i--; ) {
  2. 80  const int c_i = i;
  3. 81  if (ts[c_i].a > 0) {
  4. 82  min += ts[c_i].a*ts[c_i].x.min();
  5. 83  max += ts[c_i].a*ts[c_i].x.max();
  6. 84 } else {
  7. 85  max += ts[c_i].a*ts[c_i].x.min();
  8. 86  min += ts[c_i].a*ts[c_i].x.max();
  9. 87 }
  10. 88 }


 
et l'affichage gdb ...
 


Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x09b608d6
0x022a0134 in Gecode::MiniModel::LinExpr::post (this=0xbfffe41c, home=0x3979550, icl=ICL_DEF) at gecode/minimodel/lin-expr.cc:82
82                              min += ts[c_i].a*ts[c_i].x.min();
(gdb) print c_i
No symbol "c_i" in current context.
(gdb) print i
$1 = 3
(gdb) print n
$2 = 2


 
est-ce normal qu'il ne trouve pas la variable "c_i" ?
Et "i" a encore bougé...

Reply

Marsh Posté le 12-07-2006 à 14:14:07    

question:
 
GECODE_AUTOARRAY(Int::Linear::Term, ts, n+1);
 
si ça crée un tableau/vecteur de taille n+1, ça va donc de 0 à n en indice.
 
hors:
 
for (int i=n; i--; )
 
va de n-1 à 0 inclus en décrémentant.
 
normal - pas normal ?


Message édité par bjone le 12-07-2006 à 14:14:30
Reply

Marsh Posté le 12-07-2006 à 16:20:32    

Pour la taille des tableaux : je n'ai aucune idée si c'est normal ou pas ... La bibliothèque en question est assez mystérieuse, mal documentée et incante de la magie noire ... alors pour la question de la taille des tableaux : je n'en sais rien !!!
 
En tout cas, j'ai résolu le problème. J'avais des données mal initialisées, ce qui a du provoquer du "code aléatoire"... va savoir !
 
Merci quand même...

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed