以下のプログラムを実行すると main関数のエラーのところで終わってしまう。 コマンドプロントは以下のとおりです /* 出力結果 C:\MinGW\users\chap08>gcc -I. -o test1b test1b.c -Wall C:\MinGW\users\chap08>gdb test1b 3 1 2 3 2 1 3 3 1 2 1 3 2 2 3 1 3 2 1 genperm1 : 3 ! = 6 1 2 3 ... genperm2 : 3 ! = 6 // 正常表示されます。以下を実行するとエラーがでます。 C:\MinGW\users\chap08>gcc -g -otest1b test1b.c C:\MinGW\users\chap08>gdb test1b 3 GNU gdb (GDB) 7.6.1 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "mingw32". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from C:\MinGW\users\chap08\test1.exe...done. Can't attach to process. C:\MinGW\users\chap08/3: No such file or directory.//ここがおかしい (gdb) b main Breakpoint 1 at 0x4017a0: file test1.c, line 98. (gdb) r Starting program: C:\MinGW\users\chap08/test1.exe [New Thread 2220.0x2300] [New Thread 2220.0x2fb8] [New Thread 2220.0x1e74] [New Thread 2220.0x10c4] [New Thread 2220.0x1364] [New Thread 2220.0x37bc] [New Thread 2220.0x3054] Breakpoint 1, main (argc=1, argv=0xba10a8) at test1.c:98//ここがおかしいargc=2 のつもりなんですが。 98 if(argc < 2) (gdb) step 100 fprintf(stderr, "Usage : %s N < flag >\n", argv[0]);//Usage:gpAg「 (gdb) step Usage : C:\MinGW\users\chap08/test1.exe N < flag > 101 fprintf(stderr, " function : \n"); (gdb) step function : 順列 102 fprintf(stderr, " flag : time check mode (any char.)\n"); (gdb) step flag : time check mode (any char.) 103 exit(0); (gdb) step [Inferior 1 (process 2220) exited normally] (gdb) step The program is not being run. (gdb) #include <stdio.h> #include <time.h> #include <stdlib.h> long count; int flag; void showperm(int p[], int n); void showperm(int p[], int n) // 順列配列 p[ ] の内容を表示するだけです。 { int i, *q; count++; if(flag == 0) { for(i = 0, q = p; i < n; i++) printf("%d ", *q++); putchar('\n'); } } void genperm1(int p[], int n) { int *c, *pc, *q; int k, t; c = (int *)malloc(sizeof(int) * n); if(c == NULL) { fprintf(stderr, "Error : out of memory in genperm1()\n"); exit(-1); } for(k = 1, q = p, pc = c; k <= n; ) *q++ = *pc++ = k++; k = 1; pc = c; do { t = *(p + k); *(p + k) = *(q = p + ((k & 1)? *pc: 0)); *q = t; showperm(p, n); k = 1; pc = c; while(*pc == 0) *pc++ = k++; (*pc)--; } while(k < n); free((char *)c); } int nn; int *ok; void put(int p[], int pos, int k) { int j; *(p + pos) = k; if(pos == nn - 1) showperm(p, nn); else { *(ok + k) = 0; for(j = 1; j <= nn; j++) if(*(ok + j)) put(p, pos + 1, j); *(ok + k) = 1; } } void genperm2(int p[], int n) { int k; ok = (int *)malloc(sizeof(int) * (n + 1)); if(ok == NULL) { fprintf(stderr, "Error : out of memory in genperm2()\n"); exit(-1); } nn = n; for(k = 1; k <= n; k++) *(ok + k) = 1; for(k = 1; k <= n; k++) put(p, 0, k); free((char *)ok); } int main(int argc, char *argv[]) { clock_t start, Time; int *p, n; if(argc < 2) { fprintf(stderr, "Usage : %s N < flag >\n", argv[0]);//Usage:使用、使い方 fprintf(stderr, " function : 順列\n"); fprintf(stderr, " flag : time check mode (any char.)\n"); exit(0); } n = atoi(argv[1]); if(n < 2) { fprintf(stderr, "Error : illegal parameter input.\n"); exit(-1); } flag = (argc >= 3); p = (int *)malloc(n * sizeof(int)); if(p == NULL) { fprintf(stderr, "Error : out of memory.\n"); exit(-1); } count = 0; start = clock(); genperm1(p, n); Time = clock() - start; printf("genperm1 : %d ! = %ld", n, count); if(flag) printf("( %f [sec] )", (double)Time / CLK_TCK); putchar('\n'); count = 0; start = clock(); genperm2(p, n); Time = clock() - start; printf("genperm2 : %d ! = %ld", n, count); if(flag) printf("( %f [sec] )", (double)Time / CLK_TCK); putchar('\n'); return 1; }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/08/11 23:07
2017/08/14 23:27
退会済みユーザー
2017/09/01 10:49