Reputation: 1
I'm posting here because I couldn't find any help for this on Google.
I use Debian squeeze at uni to program some C and can ftp the content to work at home. But, I encountered this problem : a litte program I made compile with no warnings and run flawlessly at uni. But on my PC (GNU/Linux as well), it doesn't at all: Here are the conditions: -using "gcc -g -o -Wextra -ansi prog prog.c" -works at uni, segfault here -valgrind gives me this:
==4305== Invalid read of size 1
==4305== at 0x4EBCD60: __GI___rawmemchr (in /usr/lib/libc-2.18.so)
==4305== by 0x4EA9581: _IO_str_init_static_internal (in /usr/lib/libc-2.18.so)
==4305== by 0x4E99DB6: __isoc99_vsscanf (in /usr/lib/libc-2.18.so)
==4305== by 0x4E99D56: __isoc99_sscanf (in /usr/lib/libc-2.18.so)
==4305== by 0x400B69: main (chomp.c:108)
==4305== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==4305==
==4305==
==4305== Process terminating with default action of signal 11 (SIGSEGV)
==4305== Access not within mapped region at address 0x0
==4305== at 0x4EBCD60: __GI___rawmemchr (in /usr/lib/libc-2.18.so)
==4305== by 0x4EA9581: _IO_str_init_static_internal (in /usr/lib/libc-2.18.so)
==4305== by 0x4E99DB6: __isoc99_vsscanf (in /usr/lib/libc-2.18.so)
==4305== by 0x4E99D56: __isoc99_sscanf (in /usr/lib/libc-2.18.so)
==4305== by 0x400B69: main (chomp.c:108)
==4305== If you believe this happened as a result of a stack
==4305== overflow in your program's main thread (unlikely but
==4305== possible), you can try to increase the size of the
==4305== main thread stack using the --main-stacksize= flag.
==4305== The main thread stack size used in this run was 8388608.
Here's the program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
int** tab;
int n;
int m;
}Tablette;
typedef enum{J1,J2}Joueur;
typedef struct{
Tablette T;
Joueur J;
}Position;
typedef struct{
int x;
int y;
}Coup;
Tablette creer_tablette(int n, int m){
Tablette T;
int i,j;
T.n=n;
T.m=m;
T.tab = (int**) malloc(n * sizeof(int*));
if(T.tab==NULL){
printf("Erreur\n");
exit(1);
}
for(i=0;i<n;i++){
T.tab[i]=(int*)malloc(sizeof(int)*m);
}
for(i=0;i<n;i++){
if(T.tab[i]==NULL){
printf("Erreur\n");
exit(1);
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
T.tab[i][j]=1;
}
}
}
return T;
}
void afficher_tablette(Tablette T){
int i,j;
printf(" ");
for(j=0;j<T.m;j++)
printf("%d ",j+1);
printf("\n");
for(i=0;i<T.n;i++){
printf("%d ",i+1);
for(j=0;j<T.m;j++){
if(T.tab[i][j]==1)
printf("■ ");
}
printf("\n");
}
printf("\n");
}
void manger_tablette(Tablette *T, int x, int y){
int i,j;
for(i=x;i<T->n;i++){
for(j=y;j<T->m;j++){
T->tab[i][j]=0;
}
}
}
int est_legal(Position *pos, Coup *cp){
if((cp->x)<0 || (cp->x)>(pos->T.m) || (cp->y)<0 || (cp->y)>(pos->T.n))
return 0;
if(pos->T.tab[cp->y][cp->x]==0)
return 0;
return 1;
}
int fin_jeu(Position *pos, Joueur *win){
if(pos->T.tab[0][0]==0){
*win=pos->J;
return 1;
}
return 0;
}
void jouer_coup(Position *pos, Coup *cp){
if(est_legal(pos,cp)==1)
manger_tablette(&(pos->T),(cp->y-1),(cp->x-1));
pos->J=(pos->J+1)%2;
}
int main(int argc, char *argv[]){
int n,m;
sscanf(argv[2],"%d",&m);
sscanf(argv[1],"%d",&n);
Tablette T=creer_tablette(n,m);
Coup cp;
Position pos;
Joueur win;
pos.T=T;
pos.J=J1;
while(fin_jeu(&pos,&win)!=1){
afficher_tablette(T);
printf("Au tour du Joueur %d\n",pos.J+1);
printf("Entrez les coordonnées du carré que vous souhaitez manger\n");
scanf("%d",&(cp.x));
scanf("%d",&(cp.y));
while(est_legal(&pos,&cp)==0){
printf("Coordonnées non valides\n");
scanf("%d",&(cp.x));
scanf("%d",&(cp.y));
}
jouer_coup(&pos,&cp);
}
printf("Le Joueur %d a gagné !\n",pos.J+1);
return 0;
}
Thanks for your attention.
Upvotes: 0
Views: 153
Reputation: 3324
T.tab has a size of m*n
. Therefore this check is invalid.
if((cp->x)<0 || (cp->x)>(pos->T.m) || (cp->y)<0 || (cp->y)>(pos->T.n))
Change it to
if((cp->x)<0 || (cp->x)>=(pos->T.m) || (cp->y)<0 || (cp->y)>(pos->=T.n))
Upvotes: 1
Reputation: 145829
for(i=0;i<n;i++){
if(T.tab[i]==NULL){
printf("Erreur\n");
exit(1);
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
T.tab[i][j]=1;
}
}
}
You have a {
and }
bug. The second for
loop is inside the first for
loop and both modifies i
.
Upvotes: 3