| | |
access violation(segmentational fault) gah
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> void showBoard(int pLife[],int pType[]) {// WORKS NO-TOUCHY! int i; printf("\n*-----------------------------------*\n"); printf("| | | | | | |\n"); for(i=0;i<=5;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n"); printf("| | | | | | |\n"); printf("|-----+-----+-----+-----+-----+-----|\n"); printf("| | | | | | |\n"); for(i=6;i<=11;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n"); printf("| | | | | | |\n"); printf("|-----+-----+-----+-----+-----+-----|\n"); printf("| | | | | | |\n"); for(i=12;i<=17;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n"); printf("| | | | | | |\n"); printf("|-----+-----+-----+-----+-----+-----|\n"); printf("| | | | | | |\n"); for(i=18;i<=23;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n"); printf("| | | | | | |\n"); printf("|-----+-----+-----+-----+-----+-----|\n"); printf("| | | | | | |\n"); for(i=24;i<=29;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n"); printf("| | | | | | |\n"); printf("|-----+-----+-----+-----+-----+-----|\n"); printf("| | | | | | |\n"); for(i=30;i<=35;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n"); printf("| | | | | | |\n"); printf("*-----------------------------------*\n\n"); } void manualEntry(int pLife[],int pType[],int pPos,int wantedLife,int wantedType) {// WORKS NO-TOUCHY! pLife[pPos]=wantedLife; pType[pPos]=wantedType; } void resetBoardLife(int pLife[],int wantedLife) {// WORKS NO-TOUCHY! short i; for(i=0;i<=35;i++) { pLife[i]=wantedLife; } } void resetBoardType(int pType[],int wantedType) {// WORKS NO-TOUCHY! short i; for(i=0;i<=35;i++) { pType[i]=wantedType; } } int getPieceCount(int pLife[]) {// WORKS NO-TOUCHY! int i; int pieceCount=0; for(i=0;i<=35;i++) { if(pLife[i]!=0) { pieceCount+=1; } } return pieceCount; } void boardPOut(int pType[],int pLife[],int pOut[36][36],int pOutLength[]) { //WORKS NO TOUCHY int i; for(i=0;i<36;i++) { if(pLife[i]>0) { moveList(pType[i],i,pOut,pOutLength); } } } void showPOut(int pPos,int pOut[36][36],int pOutLength[]) { //WORKS NO TOUCHY int i; for(i=0;i<pOutLength[pPos];i++) { printf("%d:%d ",i,pOut[pPos][i]); } } void setStartingPosition(int startingPosition[],int *startingPositionAmount) {// Testime int i=0; int exitArray=1; int enteredValue1; printf("%d: ",i+1); scanf("%d",&enteredValue1); startingPosition[i]=enteredValue1; do { printf("Add more?(YES=1 NO=0)"); scanf("%d",&exitArray); i+=1; if(exitArray==1) { printf("%d: ",i+1); scanf("%d",&enteredValue1); startingPosition[i]=enteredValue1; } else { *startingPositionAmount=i; } } while(exitArray==1); printf("\n"); } void testStartPos(int startingPosition[],int startingPositionAmount) {// Testime int i; for(i=0;i<startingPositionAmount;i++) { printf("%d:%d ",i,startingPosition[i]); } printf("\n"); } void solver1(int pLife[],int pOut[36][36],int pOutLength[],int startingPosition[],int startingPositionAmount,int bestPath[36],int *bestPathLength) { // incomplete of course int i,x,a,b,c,d,e; int currentPos;int currentPath[36]; int dummyPLife[36]; int hasDecreased=0; for(e=0;e<36;dummyPLife[e]=pLife[e],e++); *bestPathLength=0; for(i=0;i<startingPositionAmount;i++){ if(dummyPLife[startingPosition[i]]>0) { currentPath[currentPos]=startingPosition[i]; dummyPLife[startingPosition[i]]-=1; currentPos+=1; for(x=0;x<999;x++) { // 999 is a devil standing on its head so i had to pick it :D if(currentPos==0){ //exit this loop if currentPos is at 0 break; } if(x<pOutLength[currentPath[currentPos-1]]) { if(dummyPLife[pOut[currentPath[currentPos-1]][x]]>0) { //go up 1 level currentPath[currentPos]=pOut[currentPath[currentPos-1]][x]; dummyPLife[currentPath[currentPos]]-=1; //reduce 1 dummyPLife currentPos+=1; //new currentPos x=0; //reseted to 0 for loop } } else { // go down 1 level if(currentPos>*bestPathLength) { //update bestPathLength and bestPath (if its better) *bestPathLength=currentPos; for(a=0;a<*bestPathLength;a++) { bestPath[a]=currentPath[a]; } } currentPos-=1; //currentPos lower by 1 dummyPLife[currentPath[currentPos]]+=1; //put back latest dummyPLife for(c=0;c<pOutLength[currentPath[currentPos]];c++) { if(currentPath[currentPos]==pOut[currentPath[currentPos]][c]) { x=c; break; } } } } } } printf("\n"); printf("bestPathLength = %d\n",*bestPathLength); printf("%d",bestPath[0]); for(d=1;d<*bestPathLength;d++) { printf("-%d",bestPath[d]); } } void testMoveList(int pType,int pPosition,int pOut[][],int pOutLength[]) { switch(pType) { case 0: switch(pPosition) { case 0:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 1:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 2:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 3:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 4:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 5:printf("pType=%d, pPosition=%d",pType,pPosition);return; default:printf("too far >:D"); } case 1: switch(pPosition) { case 0:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 1:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 2:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 3:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 4:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 5:printf("pType=%d, pPosition=%d",pType,pPosition);return; default:printf("too far >:D"); } case 2: switch(pPosition) { case 0:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 1:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 2:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 3:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 4:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 5:printf("pType=%d, pPosition=%d",pType,pPosition);return; default:printf("too far >:D"); } case 3: switch(pPosition) { case 0:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 1:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 2:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 3:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 4:printf("pType=%d, pPosition=%d",pType,pPosition);return; case 5:printf("pType=%d, pPosition=%d",pType,pPosition);return; default:printf("too far >:D"); } default:printf("too far >:D"); } } int main(int argc,char *argv[]) { int quit=0; int pLife[36]; int pType[36]; int startingPosition[36]; int startingPositionAmount=0; int pOut[36][36]; int pOutLength[36]; int i; int bestPath[36]; int bestPathLength; printf("Welcome...\n"); resetBoardLife(pLife,0); resetBoardType(pType,0); showBoard(pLife,pType); do { int enteredCommand; int enteredValue1; int enteredValue2; int enteredValue3; printf("+----(1)----+-----(2)-----+-----(3)------+-----(4)------+-----(5)-----+\n"); printf("|manualEntry| setStartPos |resetBoardLife|resetBoardType| solver1 |\n"); printf("+====(6)====+=====(7)=====+=====(8)======+=====(9)======+=====(10)====+\n"); printf("| | boardPOut | showPOut | moveList | |\n"); printf("+----(11)---+-----(12)----+-----(13)-----+-----(14)-----+-----(15)----+\n"); printf("| |getPieceCount| testStartPos | testMoveList | quit |\n"); printf("+-----------+-------------+--------------+--------------+-------------+\n"); scanf("%d",&enteredCommand); switch (enteredCommand) { case 1:printf("Location:"); scanf("%d",&enteredValue1); printf(" pLife:"); scanf("%d",&enteredValue2); printf(" pType:"); scanf("%d",&enteredValue3); manualEntry(pLife,pType,enteredValue1,enteredValue2,enteredValue3); break; case 2:setStartingPosition(startingPosition,&startingPositionAmount); break; case 3:printf(" pLife:"); scanf("%d",&enteredValue1); resetBoardLife(pLife,enteredValue1); break; case 4:printf(" pType:"); scanf("%d",&enteredValue1); resetBoardType(pType,enteredValue1); break; case 5:solver1(pLife,pOut,pOutLength,startingPosition,startingPositionAmount,bestPath,&bestPathLength); break; case 7: boardPOut(pType,pLife,pOut,pOutLength); break; case 8:printf("pPosition to printf array:"); scanf("%d",&enteredValue1); showPOut(enteredValue1,pOut,pOutLength); break; case 9:printf("pType(0-8):"); scanf("%d",&enteredValue1); printf("pPosition(0-35):"); scanf("%d",&enteredValue2); moveList(enteredValue1,enteredValue2,pOut,pOutLength); break; case 12:printf("There is %d pieces left.",getPieceCount(pLife)); break; case 13:testStartPos(startingPosition,startingPositionAmount); break; case 14:printf("pType(0-8):"); scanf("%d",&enteredValue1); printf("pPosition(0-5):"); scanf("%d",&enteredValue2); testMoveList(enteredValue1,enteredValue2,pOut,pOutLength); break; case 15:quit=1; break; default:printf("error\n\n"); } showBoard(pLife,pType); } while(quit==0); return 0; }
Ok heres the code, now heres where the problem is..
C Syntax (Toggle Plain Text)
void solver1(int pLife[],int pOut[36][36],int pOutLength[],int startingPosition[],int startingPositionAmount,int bestPath[36],int *bestPathLength) { // incomplete of course int i,x,a,b,c,d,e; int currentPos;int currentPath[36]; int dummyPLife[36]; int hasDecreased=0; for(e=0;e<36;dummyPLife[e]=pLife[e],e++); *bestPathLength=0; for(i=0;i<startingPositionAmount;i++){ if(dummyPLife[startingPosition[i]]>0) { currentPath[currentPos]=startingPosition[i]; dummyPLife[startingPosition[i]]-=1; currentPos+=1; for(x=0;x<999;x++) { // 999 is a devil standing on its head so i had to pick it :D if(currentPos==0){ //exit this loop if currentPos is at 0 break; } if(x<pOutLength[currentPath[currentPos-1]]) { if(dummyPLife[pOut[currentPath[currentPos-1]][x]]>0) { //go up 1 level currentPath[currentPos]=pOut[currentPath[currentPos-1]][x]; dummyPLife[currentPath[currentPos]]-=1; //reduce 1 dummyPLife currentPos+=1; //new currentPos x=0; //reseted to 0 for loop } } else { // go down 1 level if(currentPos>*bestPathLength) { //update bestPathLength and bestPath (if its better) *bestPathLength=currentPos; for(a=0;a<*bestPathLength;a++) { bestPath[a]=currentPath[a]; } } currentPos-=1; //currentPos lower by 1 dummyPLife[currentPath[currentPos]]+=1; //put back latest dummyPLife for(c=0;c<pOutLength[currentPath[currentPos]];c++) { if(currentPath[currentPos]==pOut[currentPath[currentPos]][c]) { x=c; break; } } } } } } printf("\n"); printf("bestPathLength = %d\n",*bestPathLength); printf("%d",bestPath[0]); for(d=1;d<*bestPathLength;d++) { printf("-%d",bestPath[d]); } }
The error is: An access violation(segmentational fault) on line 20 of this block down here.
This program checks a grid of 6x6, each square has a life(0~2) and a piece type(1,2,3,4,horse,bishop,tower,queen,and teleport to any other piece). The goal of this is to hit the most pieces possible...
Im pretty new to programming, so this error must come from pointers.. or bad logistic. :[
Please guide me on what it could be. Thanks

Any tips welcome! Even if it doesnt solve this. =D
Last edited by Vao; Jun 27th, 2008 at 1:20 am.
•
•
•
•
Any tips welcome! Even if it doesnt solve this. =D
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> void showBoard(int pLife[],int pType[]) { //Simply printf the whole board int i; printf("\n*-----------------------------------*\n"); printf("| | | | | | |\n"); for(i=0;i<=5;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n"); printf("| | | | | | |\n"); printf("|-----+-----+-----+-----+-----+-----|\n"); printf("| | | | | | |\n"); for(i=6;i<=11;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n"); printf("| | | | | | |\n"); printf("|-----+-----+-----+-----+-----+-----|\n"); printf("| | | | | | |\n"); for(i=12;i<=17;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n"); printf("| | | | | | |\n"); printf("|-----+-----+-----+-----+-----+-----|\n"); printf("| | | | | | |\n"); for(i=18;i<=23;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n"); printf("| | | | | | |\n"); printf("|-----+-----+-----+-----+-----+-----|\n"); printf("| | | | | | |\n"); for(i=24;i<=29;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n"); printf("| | | | | | |\n"); printf("|-----+-----+-----+-----+-----+-----|\n"); printf("| | | | | | |\n"); for(i=30;i<=35;i++) {printf("| %d-%d ",pLife[i],pType[i]);} printf("|\n"); printf("| | | | | | |\n"); printf("*-----------------------------------*\n\n"); } void manualEntry(int pLife[],int pType[],int pPos,int wantedLife,int wantedType) { //input at position X , Life Y and Type Z pLife[pPos]=wantedLife; pType[pPos]=wantedType; } void resetBoardLife(int pLife[],int wantedLife) { //modify all 36 board square Life. short i; for(i=0;i<=35;i++) { pLife[i]=wantedLife; } } void resetBoardType(int pType[],int wantedType) {//modify all 36 board square Type short i; for(i=0;i<=35;i++) { pType[i]=wantedType; } } void boardPOut(int pType[],int pLife[],int pOut[36][36],int pOutLength[]) { //=NEEDED FOR solver1= Used to get all 36 square's pOut list (list of moves that it can play if hitted int i; for(i=0;i<36;i++) { if(pLife[i]>0) { moveList(pType[i],i,pOut,pOutLength); } } } void setStartingPosition(int startingPosition[],int *startingPositionAmount) {// =NEEDED FOR solver1= define solver1 loop starting locations (the only first pieces to be played) int i=0; int exitArray=1; int enteredValue1; printf("%d: ",i+1); scanf("%d",&enteredValue1); startingPosition[i]=enteredValue1; do { printf("Add more?(YES=1 NO=0)"); scanf("%d",&exitArray); i+=1; if(exitArray==1) { printf("%d: ",i+1); scanf("%d",&enteredValue1); startingPosition[i]=enteredValue1; } else { *startingPositionAmount=i; } } while(exitArray==1); printf("\n"); } void solver1(int pLife[],int pOut[36][36],int pOutLength[],int startingPosition[],int startingPositionAmount,int bestPath[36],int *bestPathLength) { //Find the longest bestPathLength and output bestPath[] with it. int i,x,a,b,c,d,e; int currentPos; //This points to the active currentPath, starting position are the only currentPath[0] for example int currentPath[36]; //The current paths are in this, int dummyPLife[36]; //this is just a copy of the pLife array (which store each board square life) for(e=0;e<36;dummyPLife[e]=pLife[e],e++); *bestPathLength=0; for(i=0;i<startingPositionAmount;i++){ //this is the first loop, its to get all currentPath[0] from starting locations if(dummyPLife[startingPosition[i]]>0) { //if that piece life is >0 continu.. currentPath[currentPos]=startingPosition[i]; //then put this valid piece in currentPath[0] dummyPLife[startingPosition[i]]-=1; //reduce by 1 life so its not played anymore currentPos+=1; //update currentPos for(x=0;x<999;x++) { //main loop, X gets changed to 0 if a new piece can be played, or a previous value if it gets decreased if(currentPos==0){ //exit this loop if currentPos is at 0 because only starting position are at 0 break; } if(x<pOutLength[currentPath[currentPos-1]]) { //=ERROR IS HERE=if X is smaller than the pOutLength of the previous currentPath continu.. if(dummyPLife[pOut[currentPath[currentPos-1]][x]]>0) { //life is >0? go up 1 level currentPath[currentPos]=pOut[currentPath[currentPos-1]][x]; //save currentPath dummyPLife[currentPath[currentPos]]-=1; //reduce 1 dummyPLife currentPos+=1; //new currentPos x=0; //reseted to 0 for loop } } else { //go down 1 level but there is no more moves on this square if(currentPos>*bestPathLength) { //update bestPathLength and bestPath (if its better) *bestPathLength=currentPos; for(a=0;a<*bestPathLength;a++) { bestPath[a]=currentPath[a]; } } currentPos-=1; //currentPos lower by 1 to backtrack dummyPLife[currentPath[currentPos]]+=1; //put back latest dummyPLife since its not played anymore for(c=0;c<pOutLength[currentPath[currentPos]];c++) { //search the X the currentPath, if it matchs, x=c if(currentPath[currentPos]==pOut[currentPath[currentPos]][c]) { x=c; break; } } } } } } printf("\n"); //output solution =) printf("bestPathLength = %d\n",*bestPathLength); printf("%d",bestPath[0]); for(d=1;d<*bestPathLength;d++) { printf("-%d",bestPath[d]); } } int main(int argc,char *argv[]) { // main is simply used for the command entry for console int quit=0; int pLife[36]; int pType[36]; int startingPosition[36]; int startingPositionAmount=0; int pOut[36][36]; int pOutLength[36]; int i; int bestPath[36]; int bestPathLength; printf("Welcome...\n"); resetBoardLife(pLife,0); resetBoardType(pType,0); showBoard(pLife,pType); do { int enteredCommand; int enteredValue1; int enteredValue2; int enteredValue3; printf("+----(1)----+----(2)----+-----(3)------+-----(4)------+---(5)---+\n"); printf("|manualEntry|setStartPos|resetBoardLife|resetBoardType| solver1 |\n"); printf("+====(6)====+====(7)====+=====(8)======+=====(9)======+===(10)==+\n"); printf("| | boardPOut | | moveList | |\n"); printf("+----(11)---+----(12)---+-----(13)-----+-----(14)-----+--(15)---+\n"); printf("| | | | | quit |\n"); printf("+-----------+-----------+--------------+--------------+---------+\n"); scanf("%d",&enteredCommand); switch (enteredCommand) { case 1:printf("Location:"); scanf("%d",&enteredValue1); printf(" pLife:"); scanf("%d",&enteredValue2); printf(" pType:"); scanf("%d",&enteredValue3); manualEntry(pLife,pType,enteredValue1,enteredValue2,enteredValue3); break; case 2:setStartingPosition(startingPosition,&startingPositionAmount); break; case 3:printf(" pLife:"); scanf("%d",&enteredValue1); resetBoardLife(pLife,enteredValue1); break; case 4:printf(" pType:"); scanf("%d",&enteredValue1); resetBoardType(pType,enteredValue1); break; case 5:solver1(pLife,pOut,pOutLength,startingPosition,startingPositionAmount,bestPath,&bestPathLength); break; case 7: boardPOut(pType,pLife,pOut,pOutLength); break; case 9:printf("pType(0-8):"); scanf("%d",&enteredValue1); printf("pPosition(0-35):"); scanf("%d",&enteredValue2); moveList(enteredValue1,enteredValue2,pOut,pOutLength); break; case 15:quit=1; break; default:printf("error\n\n"); } showBoard(pLife,pType); } while(quit==0); return 0; }
The best i could do for relevant comments!
Ah and function moveList is this http://s219376922.onlinehome.us/misc/moveList.txt
Last edited by Vao; Jun 27th, 2008 at 12:01 pm.
![]() |
Other Threads in the C Forum
- Previous Thread: Segmentation fault--allocation error?
- Next Thread: Need help, searching & sorting
Views: 475 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function functions getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux hacking histogram homework ide include incrementoperators input intmain() iso kernel keyboard kilometer lazy license linked linkedlist linux list lists looping loopinsideloop. lowest matrix microsoft mqqueue mysql number oddnumber odf openwebfoundation overwrite pause pdf performance pointer pointers posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming spoonfeeding standard string student systemcall testing threads turboc unix urboc user variable whythiscodecausesegmentationfault windowsapi





