Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Il c non controlla!
Forum - C/C++ - Il c non controlla!

Avatar
pistilloi (Normal User)
Newbie


Messaggi: 9
Iscritto: 03/12/2010

Segnala al moderatore
Postato alle 10:10
Martedì, 10/09/2013

Come si può notare dalla sessione di debug sottopostata la variabile y assume il valore 4 pur essendo SIDE = 4 e specificato nel for y<SIDE. Come questa altre cose appaiono in memoria senza controllo...

Codice sorgente - presumibilmente C++

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4.  
  5. #define SIDE 4
  6. #define true 1
  7. #define false 0
  8.  
  9. typedef int Bool;
  10.  
  11. typedef struct Point { int x; int y; } Pnt;
  12.  
  13. typedef struct Letter{ 
  14.        
  15.         char letter;   
  16.         struct Letter * around[8];
  17.         Pnt position;
  18.        
  19. } Letter;
  20.  
  21. typedef Letter **Matrix;
  22.  
  23.  
  24. /**
  25.  *
  26.  *
  27.  */
  28. Matrix matrix_new( int N, int M ) {
  29.        
  30.         Matrix matrix;
  31.         int i;
  32.        
  33.         matrix = (Matrix) malloc( N * sizeof(Letter*) ) ;
  34.         for (i=0; i<N; ++i)
  35.                 matrix[i] = (Letter*) malloc( M * sizeof(Letter) )
  36.                         ;
  37.         return matrix ;
  38. }
  39.  
  40.  
  41. /**
  42.  *
  43.  *
  44.  */
  45. int distance_between_letters( Letter L1, Letter L2 )  {
  46.         Pnt p1 = L1.position ;
  47.         Pnt p2 = L2.position ;
  48.         return (int) sqrt(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2))
  49.                 ;
  50. }
  51.  
  52.  
  53. /**
  54.  *
  55.  *
  56.  */
  57. Letter around_of_letter( Matrix M, Letter L) {
  58.        
  59.         int x,y;
  60.         int i=0;
  61.        
  62.         for(x=0; x<SIDE; x++)
  63.                 for(y=0; y<SIDE; y++) {
  64.                        
  65.                         if( distance_between_letters( M[x][y], L ) == 1 )
  66.                                 L.around[i++] = &M[x][y]
  67.                                         ;
  68.                         else
  69.                                 L.around[i++] = NULL
  70.                                         ;
  71.                                        
  72. //                      L.position.x = x ;
  73. //                      L.position.y = y ;
  74.                 }
  75.         return L ;
  76. }
  77.  
  78.  
  79. /**
  80.  *
  81.  *
  82.  */
  83. Matrix matrix_fill( Matrix M, char *W ) {
  84.        
  85.         int x,y;
  86.         int i = 0;
  87.        
  88.         for(x=0; x<SIDE; x++)
  89.                 for(y=0; y<SIDE; y++)  {
  90.                          M[x][y].letter = W[i++];
  91.                          M[x][y].position.x = x;
  92.                          M[x][y].position.y = y;
  93.                 }
  94.         for(x=0; x<SIDE; x++)
  95.                 for(y=0; y<SIDE; y++)
  96.                         M[x][y] = around_of_letter( M, M[x][y] )
  97.                                 ;
  98.         return M ;
  99. }
  100.  
  101.  
  102. /**
  103.  *
  104.  *
  105.  */
  106. Bool search_on_matrix( Matrix M, Letter * L )  {
  107.        
  108.         int x,y;
  109.         for(x=0; x<SIDE; x++)
  110.                 for(y=0; y<SIDE; y++)  
  111.                         if( M[x][y].letter == L->letter ) {
  112.                                 L = &M[x][y] ;
  113.                                 return true ;
  114.                         }
  115.                         else
  116.                                 return false ;
  117. }
  118.  
  119. /**
  120.  *
  121.  *
  122.  */
  123. Bool search_on_around( Letter * L, char C )  {
  124.        
  125.         int i;
  126.         for(i=0; i<8; i++)
  127.  
  128.                 if( L->around[i] != NULL && L->around[i]->letter == C ) {      
  129.                         L = L->around[i] ;
  130.                         return true ;
  131.                 }
  132.                 else
  133.                         return false ;
  134. }
  135.  
  136. /**
  137.  *
  138.  *
  139.  */
  140. Bool automatic_gamer( Matrix M, char *W ) {
  141.        
  142.         int x,y;
  143.         int i = 0;
  144.         Letter L;
  145.  
  146.         do {
  147.                 switch(i) {
  148.                        
  149.                         case 0:
  150.                                
  151.                                 L.letter = W[i];
  152.                                 if( !search_on_matrix( M, &L ) )
  153.                                         return false
  154.                                                 ;
  155.                                 break;
  156.                
  157.                         default:
  158.                                
  159.                                 if( !search_on_around( &L, W[i] ) )
  160.                                         return false
  161.                                                 ;
  162.                                 break;
  163.                 }
  164.         } while( W[i++] != '\0' ) ;
  165.        
  166.         return true ;
  167. }
  168.  
  169.  
  170.  
  171. void main()  {
  172.        
  173.  Matrix M = matrix_new(4,4);
  174.  
  175.  M = matrix_fill( M, "ciaociaociaociao" ) ;
  176.                
  177.         if(automatic_gamer(M,"ciao"))
  178.                 printf("true!\n")
  179.                         ;
  180.         else printf("false!\n");
  181. }



Codice sorgente - presumibilmente Plain Text

  1. $ gdb a.out                                                                                        
  2. (gdb) list around_of_letter
  3. 54                                                                                                                                                                    
  4. 55      /**                                                                                                                                                            
  5. 56       *                                                                                                                                                            
  6. 57       *                                                                                                                                                            
  7. 58       */                                                                                                                                                            
  8. 59      Letter around_of_letter( Matrix M, Letter L) {                                                                                                                
  9. 60                                                                                                                                                                    
  10. 61              int x,y;                                                                                                                                              
  11. 62              int i=0;
  12. 63
  13. (gdb)
  14. 64              for(x=0; x<SIDE; x++)
  15. 65                      for(y=0; y<SIDE; y++) {
  16. 66
  17. 67                              if( distance_between_letters( M[x][y], L ) == 1 )
  18. 68                                      L.around[i++] = &M[x][y]
  19. 69                                              ;
  20. 70                              else
  21. 71                                      L.around[i++] = NULL
  22. 72                                              ;
  23. 73
  24. (gdb) break 64
  25. Breakpoint 1 at 0x4007de: file prova.c, line 64.
  26. (gdb) run
  27. Starting program: /home/dante/Documenti/Ruzzle/0.3/a.out
  28.  
  29. Breakpoint 1, around_of_letter (M=0x603010, L=...) at prova.c:64
  30. 64              for(x=0; x<SIDE; x++)
  31. (gdb) next
  32. 65                      for(y=0; y<SIDE; y++) {
  33. (gdb) next
  34. 67                              if( distance_between_letters( M[x][y], L ) == 1 )
  35. (gdb) print y
  36. $1 = 0
  37. (gdb) next
  38. 71                                      L.around[i++] = NULL
  39. (gdb) next
  40. 65                      for(y=0; y<SIDE; y++) {
  41. (gdb) next
  42. 67                              if( distance_between_letters( M[x][y], L ) == 1 )
  43. (gdb) print y
  44. $2 = 1
  45. (gdb) next
  46. 68                                      L.around[i++] = &M[x][y]
  47. (gdb) print y
  48. $3 = 1
  49. (gdb) next
  50. 65                      for(y=0; y<SIDE; y++) {
  51. (gdb) next
  52. 67                              if( distance_between_letters( M[x][y], L ) == 1 )
  53. (gdb) next
  54. 71                                      L.around[i++] = NULL
  55. (gdb) next
  56. 65                      for(y=0; y<SIDE; y++) {
  57. (gdb) next
  58. 67                              if( distance_between_letters( M[x][y], L ) == 1 )
  59. (gdb) next
  60. 71                                      L.around[i++] = NULL
  61. (gdb) next
  62. 65                      for(y=0; y<SIDE; y++) {
  63. (gdb) next
  64. 64              for(x=0; x<SIDE; x++)
  65. (gdb) print y
  66. $4 = 4
  67. (gdb)




PM Quote
Avatar
ZioCrocifisso (Member)
Pro


Messaggi: 135
Iscritto: 06/03/2013

Segnala al moderatore
Postato alle 11:21
Martedì, 10/09/2013
È normale, l'incremento (l'inizializzazione nel primo ciclo) avviene prima della condizione, quando y arriva a 4, viene controllato y < SIDE, e visto che non è vero, passa avanti oppure torna al loop di x, se non è terminato. Non verranno in ogni caso eseguite le istruzioni dentro al loop quando y = 4, visto che la variabile viene impostata a 0 ad ogni loop di x.

Ultima modifica effettuata da ZioCrocifisso il 10/09/2013 alle 11:23
PM Quote