Thread: "Program received signal: EXC_BAD_SIGNAL"

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    21

    "Program received signal: EXC_BAD_SIGNAL"

    Hi all, italian newbie here.

    My english is not enough to explain in words my problem, and my program is 400 lines long by now... I think I'll post only the parts of code that I think are useful...

    I typedefined some struct, and the last gives some problem to me.

    Code:
    typedef enum colore{blu, rosso}
    Team;
    /*il colore determina di quale giocatore sia il carroarmato*/
    
    typedef struct{
    	int numero;
    	Team colore;
    } Tanks;
    /*questa struct rappresenta i carro armati di una casella, 
     hanno un colore e una quantità*/
    
    typedef struct territory{
    	Tanks carriArmati;
    	struct territory* sopra; //puntatore al territorio sopra
    	struct territory* destra; //puntatore al territorio a destra
    	struct territory* sotto; //puntatore al territorio sotto
    	struct territory* sinistra; //puntatore al territorio a sinistra
    } Territory;
    The problem is to acced to the member numero of carriArmati of Territory... here's what I've done:

    Code:
    int main(int argc, char** argv)
    ..
    Territory** campo; //variabile che punta all'inizio del campo di gioco
    ..
    campo = (Territory**)malloc(sizeof(Territory)*N);
    for(i=0;i<N;i++){
    	campo[i] = (Territory*)malloc(sizeof(Territory)*M);
    }
    and that's how I declared the Territory** campo and allocated the space for it, and I'm not sure this is important, but that's how I filled it

    Code:
    for(i=0;i<N;i++){ //inizializzazione del campo
    		for(j=0;j<M;j++){
    			if(i==0) campo[i][j].sinistra = &campo[N-1][j]; //se ci troviamo nella prima colonna della matrice, il territorio adiacente a sinistra è quello nell'ultima colonna e stessa riga
                else campo[i][j].sinistra = &campo[i-1][j]; 
    			if(i==N-1) campo[i][j].destra = &campo[0][j]; //se ci troviamo nell'ultima colonna...
                else campo[i][j].destra = &campo[i+1][j];
    			if(j==0) campo[i][j].sopra = &campo[i][M-1]; //se ci troviamo nella prima riga...
                else campo[i][j].sopra = &campo[i][j-1];
    			if(j==M-1) campo[i][j].sotto = &campo[i][0]; //se ci troviamo nell'ultima riga...
                else campo[i][j].sotto = &campo[i][j+1];
    			campo[i][j].carriArmati.numero = 0;
    		}
    	}
    then I call a function like this:

    Code:
    stampaCampo(campo);
    and there, finally, there's the instruction that gives me problems:

    Code:
    if(campo[i-1][j-1].carriArmati.numero == 0)
    the whole program compile, but then it gives me:
    "Thread 1: program received signal: EXC_BAD_SIGNAL"

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    i-1 or j-1 is probably outside the bounds of your matrix.

    If you can't find the problem you'll have to at least post the code for stampaCampo.
    Last edited by oogabooga; 02-29-2012 at 11:08 AM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    Code:
    void stampaCampo(Territory** campo){
    	int i,j;
    	
    	printf("\n"); //pura estetica
    	for(i=0;i<N+1;i++){ //contarighe, ce ne vuole una in più per aggiungere la numerazione
    		for(j=0;j<M+1;j++){ //contacolonne, ce ne vuole una in più per aggiungere la numerazione
    			if(i==0){ //se ci troviamo nella primissima riga da stampare, bisogna stampare la numerazione in alto
    				if(j==0){ //nell'angolino bisogna non c'è niente da stampare
    					printf("--");
    				}else printf("%6d", j-1);
    			}else{
    			if(i>0 && j==0){ //se ci troviamo nella prima colonna ma non nell'angolino, bisogna stampare la numerazione a sinistra
    				printf(" %d", i-1);
    				//se non ci troviamo in nessuno dei casi precedenti, allora siamo in mezzo alla matrice e bisogna stampare i territori
    			}else{
    				if(campo[i-1][j-1].carriArmati.numero == 0){
    					printf("    -V"); //se in quel territorio non ci sono carri armati, stampane una notifica
    				}else if(campo[i-1][j-1].carriArmati.colore == blu){ //altrimenti se sono blu, stampa di conseguenza
    					printf("%6dB", campo[i-1][j-1].carriArmati.numero);
    				}else if(campo[i-1][j-1].carriArmati.colore == rosso){ //o se sono rossi, stampa di conseguenza
    					printf("%6dR", campo[i-1][j-1].carriArmati.numero);
    				}
    			}
                }
    			
    		}
            printf("\n");
    	}
    	printf("\n");
    	return;
    }

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try this. Note the assert statements. Remember to #include <assert.h>.
    I think that j!=0 will print because j will be zero at one point in that section. Then your j-1 access will be an error.
    Code:
    void stampaCampo(Territory** campo){
    	int i,j;
    	
    	printf("\n"); //pura estetica
    	for(i=0;i<N+1;i++){ //contarighe, ce ne vuole una in più per aggiungere la numerazione
    		for(j=0;j<M+1;j++){ //contacolonne, ce ne vuole una in più per aggiungere la numerazione
    			if(i==0){ //se ci troviamo nella primissima riga da stampare, bisogna stampare la numerazione in alto
    				if(j==0){ //nell'angolino bisogna non c'è niente da stampare
    					printf("--");
    				}else printf("%6d", j-1);
    			}else{
    			if(i>0 && j==0){ //se ci troviamo nella prima colonna ma non nell'angolino, bisogna stampare la numerazione a sinistra
    				printf(" %d", i-1);
    				//se non ci troviamo in nessuno dei casi precedenti, allora siamo in mezzo alla matrice e bisogna stampare i territori
    			}else{
    
    				assert(i!=0);
    				assert(j!=0);
    
    				if(campo[i-1][j-1].carriArmati.numero == 0){
    					printf("    -V"); //se in quel territorio non ci sono carri armati, stampane una notifica
    				}else if(campo[i-1][j-1].carriArmati.colore == blu){ //altrimenti se sono blu, stampa di conseguenza
    					printf("%6dB", campo[i-1][j-1].carriArmati.numero);
    				}else if(campo[i-1][j-1].carriArmati.colore == rosso){ //o se sono rossi, stampa di conseguenza
    					printf("%6dR", campo[i-1][j-1].carriArmati.numero);
    				}
    			}
                }
    			
    		}
            printf("\n");
    	}
    	printf("\n");
    	return;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 24
    Last Post: 02-09-2011, 09:19 AM
  2. "exited due to signal 10 (sigbus)"
    By pktcperlc++java in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2005, 10:07 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. signal not received
    By Its me! in forum C Programming
    Replies: 4
    Last Post: 10-08-2001, 07:04 PM