• Jueves 28 de Marzo de 2024, 10:20

Autor Tema:  Gestion dinamica de memoria  (Leído 661 veces)

keyboardwizz

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
Gestion dinamica de memoria
« en: Sábado 25 de Julio de 2009, 05:10 »
0
Tengo las siguientes funciones:

Código: Text
  1.  
  2. int validMove(const char board[7][7], char direccion, int x,int y){
  3.     switch (direccion){
  4.         case 'N':
  5.             if(y>1 && board[y-1][x]=='1' && board[y-2][x]=='1')
  6.                 return 1;
  7.             break;
  8.         case 'S':
  9.             if(y<5 && board[y+1][x]=='1' && board[y+2][x]=='1')
  10.                 return 1;
  11.             break;
  12.         case 'O':
  13.             if(x>1 && board[y][x-1]=='1' && board[y][x-2]=='1')
  14.                 return 1;
  15.             break;
  16.         case 'E':
  17.             if(x<5 && board[y][x+1]=='1' && board[y][x+2]=='1')
  18.                 return 1;
  19.             break;
  20.         default:
  21.             printf("Mal llamada de la funcion");
  22.     }
  23. }
  24.  
  25. int findMoves(char** moves,const char board[7][7]){
  26.     int i,j;
  27.     int n=0;
  28.     for(i=0;i<7;++i){
  29.         for(j=0;j<7;++j){
  30.             if(board[i][j]=='0'){
  31.                 if(validMove(board,'N',j,i)){
  32.                     ++n;
  33.                     moves = realloc(moves,n*sizeof(char*));
  34.                     moves[n-1] = malloc(6*sizeof(char));
  35.                     sprintf(moves[n-1],"%i-%i",(i-2)*10+j,i*10+j);
  36.                 }
  37.                 if(validMove(board,'S',j,i)){
  38.                     ++n;
  39.                     moves = realloc(moves,n*sizeof(char*));
  40.                     moves[n-1] = malloc(6*sizeof(char));
  41.                     sprintf(moves[n-1],"%i-%i",(i+2)*10+j,i*10+j);
  42.                 }
  43.                 if(validMove(board,'E',j,i)){
  44.                     ++n;
  45.                     moves = realloc(moves,n*sizeof(char*));
  46.                     moves[n-1] = malloc(6*sizeof(char));
  47.                     sprintf(moves[n-1],"%i-%i",i*10+j+2,i*10+j);
  48.                 }
  49.                 if(validMove(board,'O',j,i)){
  50.                     ++n;
  51.                     moves = realloc(moves,n*sizeof(char*));
  52.                     moves[n-1] = malloc(6*sizeof(char));
  53.                     sprintf(moves[n-1],"%i-%i",i*10+j-2,i*10+j);
  54.                 }
  55.             }
  56.         }
  57.     }
  58.     return n;
  59. }
  60.  
  61.  

Donde board es una matriz de este estilo:
2 2 1 1 1 2 2
2 2 1 1 1 2 2
1 1 1 1 1 1 1
1 1 1 0 1 1 1
1 1 1 1 1 1 1
2 2 1 1 1 2 2
2 2 1 1 1 2 2
Sin espacios claramente.
Es el juego del solitario y las funciones buscan los movimientos que es posible hacer.

Cuando lo corri aparentemente funciono correctamente, porque se imprimieron los 4 movimientos posibles (mover piezas al centro), pero luego hice un debug y me di cuenta que cuando entra al tercer "if" de findMoves en vez de reservar una cadena de caracteres mas reserva 5. No se me ocurre porque podría ser.