Oppure

Loading
10/09/13 8:10
pistilloi

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...

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

#define SIDE 4
#define true 1
#define false 0

typedef int Bool;

typedef struct Point { int x; int y; } Pnt;

typedef struct Letter{	
	
	char letter;	
	struct Letter * around[8]; 
	Pnt position;
	
} Letter;

typedef Letter **Matrix;


/**
 * 
 *
 */ 
Matrix matrix_new( int N, int M ) {
	
	Matrix matrix;
	int i;
	
	matrix = (Matrix) malloc( N * sizeof(Letter*) ) ;
	for (i=0; i<N; ++i)
		matrix[i] = (Letter*) malloc( M * sizeof(Letter) )
			;
	return matrix ;
}


/**
 * 
 *
 */ 
int distance_between_letters( Letter L1, Letter L2 )  {
	Pnt p1 = L1.position ;
	Pnt p2 = L2.position ;
	return (int) sqrt(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2))
		;
}


/**
 * 
 *
 */
Letter around_of_letter( Matrix M, Letter L) {
	
	int x,y;
	int i=0;
	
	for(x=0; x<SIDE; x++)
		for(y=0; y<SIDE; y++) {
			
			if( distance_between_letters( M[x][y], L ) == 1 ) 
				L.around[i++] = &M[x][y]
					;
			else 
				L.around[i++] = NULL
					;
					
// 			L.position.x = x ;
// 			L.position.y = y ;
		}
	return L ; 
}


/**
 * 
 * 
 */
Matrix matrix_fill( Matrix M, char *W ) {
	
	int x,y;
	int i = 0;
	
	for(x=0; x<SIDE; x++)
		for(y=0; y<SIDE; y++)  { 
			 M[x][y].letter = W[i++];
			 M[x][y].position.x = x;
			 M[x][y].position.y = y;
		}
	for(x=0; x<SIDE; x++)
		for(y=0; y<SIDE; y++)
			M[x][y] = around_of_letter( M, M[x][y] ) 
				; 
	return M ;
}


/**
 * 
 *
 */
Bool search_on_matrix( Matrix M, Letter * L )  {
	
	int x,y;
	for(x=0; x<SIDE; x++)
		for(y=0; y<SIDE; y++)  
			if( M[x][y].letter == L->letter ) {
				L = &M[x][y] ;
				return true ;
			}
			else 
				return false ;
}

/**
 * 
 *
 */
Bool search_on_around( Letter * L, char C )  {
	
	int i;
	for(i=0; i<8; i++) 

		if( L->around[i] != NULL && L->around[i]->letter == C )	{	
			L = L->around[i] ; 
			return true ;
		}
		else 
			return false ;
}

/**
 * 
 *
 */
Bool automatic_gamer( Matrix M, char *W ) {
	
	int x,y;
	int i = 0;
	Letter L;

	do {
		switch(i) {
			
			case 0:
				
				L.letter = W[i];
				if( !search_on_matrix( M, &L ) )
					return false 
						;
				break;
		
			default:
				
				if( !search_on_around( &L, W[i] ) )
					return false 
						;
				break;
		}
	} while( W[i++] != '

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...

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

#define SIDE 4
#define true 1
#define false 0

typedef int Bool;

typedef struct Point { int x; int y; } Pnt;

typedef struct Letter{	
	
	char letter;	
	struct Letter * around[8]; 
	Pnt position;
	
} Letter;

typedef Letter **Matrix;


/**
 * 
 *
 */ 
Matrix matrix_new( int N, int M ) {
	
	Matrix matrix;
	int i;
	
	matrix = (Matrix) malloc( N * sizeof(Letter*) ) ;
	for (i=0; i<N; ++i)
		matrix[i] = (Letter*) malloc( M * sizeof(Letter) )
			;
	return matrix ;
}


/**
 * 
 *
 */ 
int distance_between_letters( Letter L1, Letter L2 )  {
	Pnt p1 = L1.position ;
	Pnt p2 = L2.position ;
	return (int) sqrt(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2))
		;
}


/**
 * 
 *
 */
Letter around_of_letter( Matrix M, Letter L) {
	
	int x,y;
	int i=0;
	
	for(x=0; x<SIDE; x++)
		for(y=0; y<SIDE; y++) {
			
			if( distance_between_letters( M[x][y], L ) == 1 ) 
				L.around[i++] = &M[x][y]
					;
			else 
				L.around[i++] = NULL
					;
					
// 			L.position.x = x ;
// 			L.position.y = y ;
		}
	return L ; 
}


/**
 * 
 * 
 */
Matrix matrix_fill( Matrix M, char *W ) {
	
	int x,y;
	int i = 0;
	
	for(x=0; x<SIDE; x++)
		for(y=0; y<SIDE; y++)  { 
			 M[x][y].letter = W[i++];
			 M[x][y].position.x = x;
			 M[x][y].position.y = y;
		}
	for(x=0; x<SIDE; x++)
		for(y=0; y<SIDE; y++)
			M[x][y] = around_of_letter( M, M[x][y] ) 
				; 
	return M ;
}


/**
 * 
 *
 */
Bool search_on_matrix( Matrix M, Letter * L )  {
	
	int x,y;
	for(x=0; x<SIDE; x++)
		for(y=0; y<SIDE; y++)  
			if( M[x][y].letter == L->letter ) {
				L = &M[x][y] ;
				return true ;
			}
			else 
				return false ;
}

/**
 * 
 *
 */
Bool search_on_around( Letter * L, char C )  {
	
	int i;
	for(i=0; i<8; i++) 

		if( L->around[i] != NULL && L->around[i]->letter == C )	{	
			L = L->around[i] ; 
			return true ;
		}
		else 
			return false ;
}

/**
 * 
 *
 */
Bool automatic_gamer( Matrix M, char *W ) {
	
	int x,y;
	int i = 0;
	Letter L;

	do {
		switch(i) {
			
			case 0:
				
				L.letter = W[i];
				if( !search_on_matrix( M, &L ) )
					return false 
						;
				break;
		
			default:
				
				if( !search_on_around( &L, W[i] ) )
					return false 
						;
				break;
		}
	} while( W[i++] != '{parsed_message}' ) ;
	
	return true ;
}



void main()  {
	
 Matrix M = matrix_new(4,4);
 
 M = matrix_fill( M, "ciaociaociaociao" ) ;
		
	if(automatic_gamer(M,"ciao"))
		printf("true!\n")
			;
	else printf("false!\n");
}


$ gdb a.out                                                                                        
(gdb) list around_of_letter
54                                                                                                                                                                     
55      /**                                                                                                                                                            
56       *                                                                                                                                                             
57       *                                                                                                                                                             
58       */                                                                                                                                                            
59      Letter around_of_letter( Matrix M, Letter L) {                                                                                                                 
60                                                                                                                                                                     
61              int x,y;                                                                                                                                               
62              int i=0;
63
(gdb) 
64              for(x=0; x<SIDE; x++)
65                      for(y=0; y<SIDE; y++) {
66
67                              if( distance_between_letters( M[x][y], L ) == 1 ) 
68                                      L.around[i++] = &M[x][y]
69                                              ;
70                              else 
71                                      L.around[i++] = NULL
72                                              ;
73
(gdb) break 64
Breakpoint 1 at 0x4007de: file prova.c, line 64.
(gdb) run
Starting program: /home/dante/Documenti/Ruzzle/0.3/a.out 

Breakpoint 1, around_of_letter (M=0x603010, L=...) at prova.c:64
64              for(x=0; x<SIDE; x++)
(gdb) next
65                      for(y=0; y<SIDE; y++) {
(gdb) next
67                              if( distance_between_letters( M[x][y], L ) == 1 ) 
(gdb) print y
 = 0
(gdb) next
71                                      L.around[i++] = NULL
(gdb) next
65                      for(y=0; y<SIDE; y++) {
(gdb) next
67                              if( distance_between_letters( M[x][y], L ) == 1 ) 
(gdb) print y
 = 1
(gdb) next
68                                      L.around[i++] = &M[x][y]
(gdb) print y
 = 1
(gdb) next
65                      for(y=0; y<SIDE; y++) {
(gdb) next
67                              if( distance_between_letters( M[x][y], L ) == 1 ) 
(gdb) next
71                                      L.around[i++] = NULL
(gdb) next
65                      for(y=0; y<SIDE; y++) {
(gdb) next
67                              if( distance_between_letters( M[x][y], L ) == 1 ) 
(gdb) next
71                                      L.around[i++] = NULL
(gdb) next
65                      for(y=0; y<SIDE; y++) {
(gdb) next
64              for(x=0; x<SIDE; x++)
(gdb) print y
 = 4
(gdb)


' ) ; return true ; } void main() { Matrix M = matrix_new(4,4); M = matrix_fill( M, "ciaociaociaociao" ) ; if(automatic_gamer(M,"ciao")) printf("true!\n") ; else printf("false!\n"); }


$ gdb a.out                                                                                        
(gdb) list around_of_letter
54                                                                                                                                                                     
55      /**                                                                                                                                                            
56       *                                                                                                                                                             
57       *                                                                                                                                                             
58       */                                                                                                                                                            
59      Letter around_of_letter( Matrix M, Letter L) {                                                                                                                 
60                                                                                                                                                                     
61              int x,y;                                                                                                                                               
62              int i=0;
63
(gdb) 
64              for(x=0; x<SIDE; x++)
65                      for(y=0; y<SIDE; y++) {
66
67                              if( distance_between_letters( M[x][y], L ) == 1 ) 
68                                      L.around[i++] = &M[x][y]
69                                              ;
70                              else 
71                                      L.around[i++] = NULL
72                                              ;
73
(gdb) break 64
Breakpoint 1 at 0x4007de: file prova.c, line 64.
(gdb) run
Starting program: /home/dante/Documenti/Ruzzle/0.3/a.out 

Breakpoint 1, around_of_letter (M=0x603010, L=...) at prova.c:64
64              for(x=0; x<SIDE; x++)
(gdb) next
65                      for(y=0; y<SIDE; y++) {
(gdb) next
67                              if( distance_between_letters( M[x][y], L ) == 1 ) 
(gdb) print y
 = 0
(gdb) next
71                                      L.around[i++] = NULL
(gdb) next
65                      for(y=0; y<SIDE; y++) {
(gdb) next
67                              if( distance_between_letters( M[x][y], L ) == 1 ) 
(gdb) print y
 = 1
(gdb) next
68                                      L.around[i++] = &M[x][y]
(gdb) print y
 = 1
(gdb) next
65                      for(y=0; y<SIDE; y++) {
(gdb) next
67                              if( distance_between_letters( M[x][y], L ) == 1 ) 
(gdb) next
71                                      L.around[i++] = NULL
(gdb) next
65                      for(y=0; y<SIDE; y++) {
(gdb) next
67                              if( distance_between_letters( M[x][y], L ) == 1 ) 
(gdb) next
71                                      L.around[i++] = NULL
(gdb) next
65                      for(y=0; y<SIDE; y++) {
(gdb) next
64              for(x=0; x<SIDE; x++)
(gdb) print y
 = 4
(gdb)


aaa
10/09/13 9:21
ZioCrocifisso
È 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 10/09/13 9:23
aaa