Note that there are some explanatory texts on larger screens.

plurals
  1. POC program compile and run on a system and not on another
    primarykey
    data
    text
    <p>I'm posting here because I couldn't find any help for this on Google.</p> <p>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:</p> <pre><code>==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. </code></pre> <p>Here's the program:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; 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&lt;n;i++){ T.tab[i]=(int*)malloc(sizeof(int)*m); } for(i=0;i&lt;n;i++){ if(T.tab[i]==NULL){ printf("Erreur\n"); exit(1); } for(i=0;i&lt;n;i++){ for(j=0;j&lt;m;j++){ T.tab[i][j]=1; } } } return T; } void afficher_tablette(Tablette T){ int i,j; printf(" "); for(j=0;j&lt;T.m;j++) printf("%d ",j+1); printf("\n"); for(i=0;i&lt;T.n;i++){ printf("%d ",i+1); for(j=0;j&lt;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&lt;T-&gt;n;i++){ for(j=y;j&lt;T-&gt;m;j++){ T-&gt;tab[i][j]=0; } } } int est_legal(Position *pos, Coup *cp){ if((cp-&gt;x)&lt;0 || (cp-&gt;x)&gt;(pos-&gt;T.m) || (cp-&gt;y)&lt;0 || (cp-&gt;y)&gt;(pos-&gt;T.n)) return 0; if(pos-&gt;T.tab[cp-&gt;y][cp-&gt;x]==0) return 0; return 1; } int fin_jeu(Position *pos, Joueur *win){ if(pos-&gt;T.tab[0][0]==0){ *win=pos-&gt;J; return 1; } return 0; } void jouer_coup(Position *pos, Coup *cp){ if(est_legal(pos,cp)==1) manger_tablette(&amp;(pos-&gt;T),(cp-&gt;y-1),(cp-&gt;x-1)); pos-&gt;J=(pos-&gt;J+1)%2; } int main(int argc, char *argv[]){ int n,m; sscanf(argv[2],"%d",&amp;m); sscanf(argv[1],"%d",&amp;n); Tablette T=creer_tablette(n,m); Coup cp; Position pos; Joueur win; pos.T=T; pos.J=J1; while(fin_jeu(&amp;pos,&amp;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",&amp;(cp.x)); scanf("%d",&amp;(cp.y)); while(est_legal(&amp;pos,&amp;cp)==0){ printf("Coordonnées non valides\n"); scanf("%d",&amp;(cp.x)); scanf("%d",&amp;(cp.y)); } jouer_coup(&amp;pos,&amp;cp); } printf("Le Joueur %d a gagné !\n",pos.J+1); return 0; } </code></pre> <p>Thanks for your attention.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload