c - I keep getting random segmentation faults -
i've written little bit (about 200 lines) of incomplete (should be) functioning argument handling code. incomplete, mean haven't added in conditionals arguments other ?, -h, , --help, arguments long, , arguments unrecognized.
i took upon myself stuff controlling variables 1 array , began imposing checks on values (i planned impose checks beforehand). checks prevent values leaving i've read portably acceptable type. i've added checks in though, random segmentation faults. when run compiled code segmentation fault, don't, seem same each run through. perhaps not understand invoking segmentation faults well. added in function 'pdebug' prints number specified argument, , sprinkled them @ origin , ends conditionals , loops suspected such originating from.
the next output programme on multiple run throughs:
charles@draton-generico:~/documents/c/c89/sdl_work/2d-game-base$ ./game-base-02
1
2
no arguments process.
charles@draton-generico:~/documents/c/c89/sdl_work/2d-game-base$ ./game-base-02
1
2
no arguments process.
charles@draton-generico:~/documents/c/c89/sdl_work/2d-game-base$ ./game-base-02 --help
1
3
4
segmentation fault (core dumped)
charles@draton-generico:~/documents/c/c89/sdl_work/2d-game-base$ ./game-base-02 --help
1
3
4
5
6
argv[1]: caught argument '--help'.
successfully exited argument catching loop.
every runthrough appear have went planned except third.
the next code, comment indicating think offending code or follow output of programme , pdebugs:
/* created by: charles edwin swain 3rd * date of project beginning: 28/1/2013 * notes self: * of import note lvalues inputted arguments: * -either declared outside if/else conditional before it, or in both. * -in latter of two, lvalue dictates if argument has been given twice used determine if lvalue associated has been declared. * -in end declared , sorted through eventually. * although numbering convention error homecoming values neat add-on , helpful debugging, best add together such @ end of writing this. */ #include <stdio.h> #include <stdlib.h> int pdebug(int nmb) { int chk[2]; chk[0] = -1; chk[1] = 0; if (fprintf(stdout, "%d\n", nmb) <= 0) { while ((chk[0] <= 0)&&(chk[1] < 50)) { chk[0] = fprintf(stderr, "a function (fprintf) failed.\n"); chk[1] = chk[1] + 1; } homecoming -1; } homecoming 0; } int main(int argc, char** argv) { pdebug(1); int bse[3]; bse[0] = 0; while (bse[0] < 3) { bse[bse[0]] = 0; bse[0] = bse[0] + 1; } bse[1] = -1; if (argc <= 1) { pdebug(2); if (fprintf(stdout, "no arguments process.\n") <= 0) { while ((bse[1] <= 0)&&(bse[2] < 50)) { bse[1] = fprintf(stderr, "a function (fprintf) failed while alerting user of having no arguments process.\n\twill exit.\n"); bse[2] = bse[2] + 1; } exit(-1); } /*set default values here.*/ } else { pdebug(3); int arg[argc + 6]; while (bse[0] < (argc + 6)) { arg[bse[0]] = 0; bse[0] = bse[0] + 1; } bse[0] = 0; pdebug(4); /*this sets 'arg[argc + 2]' size of largest argument string.*/ /*this segmentation faults occuring. while loop.*/ while (arg[argc + 0] < argc) { while (arg[argc + 1] != -1) { if (argv[arg[argc + 0]][arg[argc + 1]] == '\0') { arg[argc + 0] = arg[argc + 0] + 1; if ((arg[argc + 1] + 1) > arg[argc + 2]) arg[argc + 2] = arg[argc + 1] + 1; arg[argc + 1] = -1; } else if (arg[argc + 1] == 32766) { arg[arg[argc + 0]] = 1; arg[argc + 0] = arg[argc + 0] + 1; arg[argc + 1] = -1; } else arg[argc + 1] = arg[argc + 1] + 1; } arg[argc + 1] = 0; } pdebug(5); if (arg[argc + 2] < 7) arg[argc + 2] = arg[argc + 2] + (7 - arg[argc + 2]); char ahs[arg[argc + 2]]; /*if want, add together name check here.*/ arg[argc + 0] = 1; pdebug(6); /*this big argument catching loop.*/ while (arg[argc + 0] < argc) { if (arg[arg[argc + 0]] == 1) { if (fprintf(stdout, "argv[%d]: argument composed of many characters (max 32766, including null character).\n", arg[argc + 0]) <= 0) { while ((bse[1] <= 0)&&(bse[2] < 50)) { bse[1] = fprintf(stderr, "a function (fprintf) failed in argument catching loop.\n\twill exit.\n"); bse[2] = bse[2] + 1; } exit(-1); } arg[argc + 4] = 0; arg[argc + 3] = -1; } else { /*this sets contents of array 'ahs' null character.*/ while (arg[argc + 1] != arg[argc + 2]) { ahs[arg[argc + 1]] = '\0'; arg[argc + 1] = arg[argc + 1] + 1; } arg[argc + 1] = 0; /*this retreives contents of 'argv[arg[argc + 0]]' , places them in 'ahs'.*/ while (arg[argc + 1] != -1) { ahs[arg[argc + 1]] = argv[arg[argc + 0]][arg[argc + 1]]; if (ahs[arg[argc + 1]] == '\0') arg[argc + 1] = -1; else arg[argc + 1] = arg[argc + 1] + 1; } arg[argc + 1] = 0; /*these conditionals checking retreived arguments.*/ if (((arg[argc + 4] == 0)&&(arg[argc + 5] == 0))&&(((ahs[0] == '?')&&(ahs[1] == '\0'))||((ahs[0] == '-')&&(ahs[1] == 'h')&&(ahs[2] == '\0'))||((ahs[0] == '-')&&(ahs[1] == '-')&&(ahs[2] == 'h')&&(ahs[3] == 'e')&&(ahs[4] == 'l')&&(ahs[5] == 'p')&&(ahs[6] == '\0')))) { arg[argc + 5] = 1; if (fprintf(stdout, "argv[%d]: caught argument '%s'.\n", arg[argc + 0], ahs) <= 0) { while ((bse[1] <= 0)&&(bse[2] < 50)) { bse[1] = fprintf(stderr, "a function (fprintf) failed in argument catching loop.\n\twill exit.\n"); bse[2] = bse[2] + 1; } exit(-1); } } else if (((arg[argc + 4] == 0)&&(arg[argc + 5] == 1))&&(((ahs[0] == '?')&&(ahs[1] == '\0'))||((ahs[0] == '-')&&(ahs[1] == 'h')&&(ahs[2] == '\0'))||((ahs[0] == '-')&&(ahs[1] == '-')&&(ahs[2] == 'h')&&(ahs[3] == 'e')&&(ahs[4] == 'l')&&(ahs[5] == 'p')&&(ahs[6] == '\0')))) { arg[argc + 3] = -1; if (fprintf(stdout, "argv[%d]: duplicate synonym '?', '-h', or '--help' argument.\n", arg[argc + 0]) <= 0) { while ((bse[1] <= 0)&&(bse[2] < 50)) { bse[1] = fprintf(stderr, "a function (fprintf) failed in argument catching loop.\n\twill exit.\n"); bse[2] = bse[2] + 1; } exit(-1); } } else { arg[argc + 3] = -1; if (fprintf(stdout, "argv[%d]: unrecognized argument '%s'.\n", arg[argc + 0], ahs) <= 0) { while ((bse[1] <= 0)&&(bse[2] < 50)) { bse[1] = fprintf(stderr, "a function (fprintf) failed in argument catching loop.\n\twill exit.\n"); bse[2] = bse[2] + 1; } exit(-1); } } } arg[argc + 1] = 0; arg[argc + 0] = arg[argc + 0] + 1; } if (arg[argc + 3] == -1) { if (fprintf(stdout, "argument catcher has asked exit called after exiting argument catching loop.\n\twill exit.\n") <= 0) { while ((bse[1] <= 0)&&(bse[2] < 50)) { bse[1] = fprintf(stderr, "a function (fprintf) failed when outputting notification informing impending exit demand of argument catcher in argument catching loop.\n\twill exit.\n"); bse[2] = bse[2] + 1; } exit(-1); } exit(-1); } if (fprintf(stdout, "successfully exited argument catching loop.\n") <= 0) { while ((bse[1] <= 0)&&(bse[2] < 50)) { bse[1] = fprintf(stderr, "a function (fprintf) failed when outputting notification informing of having 'properly' left argument catching loop.\n\twill exit.\n"); bse[2] = bse[2] + 1; } exit(-1); } /*set default values here.*/ } /*environment setup here.*/ /*end of code.*/ exit(0); }
i have no thought why producing segmentation faults, chance @ turning learning experience helpful, please explain. also, should set cap on number of arguments accepted?
i took few moments step through code in debugger (it wasn't hard), , determined arg[0], arg[1], arg[2] never initialized in loop (line 60):
int arg[argc + 6]; while (bse[0] < (argc + 6)) { arg[bse[0]] = 0; bse[0] = bse[0] + 1; }
that because bse[0] = 3, because of code here (line 36):
while (bse[0] < 3) { bse[bse[0]] = 0; bse[0] = bse[0] + 1; }
i offer assistance in rewriting, can't figure out heck doing
c segmentation-fault command-line-arguments
No comments:
Post a Comment