From: Walter Luke Newsgroups: comp.os.msdos.djgpp Subject: Why does this code crash? Date: Mon, 20 Jul 1998 08:33:31 -0400 Organization: infocom, inc. Lines: 250 Message-ID: <35B3391A.227330DD@earlham.edu> Reply-To: lukewa AT earlham DOT edu NNTP-Posting-Host: dhcp-host509.cclab.earlham.edu Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="------------B336608AE7D6093C8540CF1E" To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk --------------B336608AE7D6093C8540CF1E Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit I apologize if the problem I'm having is simple--I'm new to C. The following code generates a general protection fault and crashes the DOS window. The error message reads, among other things, "Exiting due to signal SIGSEGV". If someone could look over this and try to see where the error is, it would be a big help... --Walter > #include "math.h" > #include "stdio.h" > #include "stdlib.h" > #include "dos.h" > #include "stdlib.h" > > int gene[500][9][2]; > int genes=2; > int population=1; > int totattributes=2; > int genepool=0; > int i, j, o, p, g, h; > FILE *fp; > > main() { > > if ((fp = fopen("log.txt","w"))==NULL) { > printf("cannot open file\n"); > exit(1); > } > //initializes genes > for(i=1; i<=genes; i++) { //initializes non-conditional genes > gene[i][2][1]=1; > gene[i][7][1]=i; > genepool++; > } > > genepool++; > > for(i=1; i<=population; i++) { //initializes conditional genes > gene[genepool][1][1]=i; //owning agent > gene[genepool][2][1]=2; //marks it as a conditional > for(j=1; j<=2; j++) { > gene[genepool][3][1]=j; //agent vs agent's attributes stimulus > if (j==2){ > for(o=1; o<=totattributes; p++){ > gene[genepool][4][1]=o; > for (p=1; p<=population; p++){ > gene[genepool][5][1]=p; > gene[genepool][6][1]=1; > if (j==1) { > for(g=1; g<=genes; g++){ > gene[genepool][7][1]=g; > for(h=1; h<=genes; h++) { > gene[genepool][8][1]=h; > genepool++; > } > } > } > if (j==2) { > for(g=1; g<=totattributes; g++) { > gene[genepool][7][1]=g; > for(h=1; h<=genes; h++) { > gene[genepool][8][1]=h; > genepool++; > } > } > } > } > } > } > if (j==1){ //second one > > gene[genepool][4][1]=0; > for (p=1; p<=population; p++){ > gene[genepool][5][1]=p; > gene[genepool][6][1]=1; > if (j==1) { > for(g=1; g<=genes; g++){ > gene[genepool][7][1]=g; > for(h=1; h<=genes; h++) { > gene[genepool][8][1]=h; > genepool++; > } > } > } > if (j==2) { > for(g=1; g<=totattributes; g++) { > gene[genepool][7][1]=g; > for(h=1; h<=genes; h++) { > gene[genepool][8][1]=h; > genepool++; > } > } > } > } > > } > } > } > > > for(i=1; i<=genepool; i++){ > for(j=1; j<=8; j++){ > fprintf(fp,"gene %d: {",i); > fprintf(fp,"%d,", gene[i][j][1]); > } > fprintf(fp,"}"); > } > > > > fclose(fp); > > > } > --------------B336608AE7D6093C8540CF1E Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit    I apologize if the problem I'm having is simple--I'm new to C. The following code generates a general protection fault and crashes the DOS window. The error message reads, among other things, "Exiting due to signal SIGSEGV". If someone could look over this and try to see where the error is, it would be a big help...

                                                                                                                                --Walter
 

#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#include "dos.h"
#include "stdlib.h"

int gene[500][9][2];
int genes=2;
int population=1;
int totattributes=2;
int genepool=0;
int i, j, o, p, g, h;
FILE *fp;

main() {

  if ((fp = fopen("log.txt","w"))==NULL) {
    printf("cannot open file\n");
    exit(1);
  }
                  //initializes genes
  for(i=1; i<=genes; i++) {   //initializes non-conditional genes
    gene[i][2][1]=1;
    gene[i][7][1]=i;
    genepool++;
  }

  genepool++;

  for(i=1; i<=population; i++) {  //initializes conditional genes
    gene[genepool][1][1]=i;  //owning agent
    gene[genepool][2][1]=2;  //marks it as a conditional
    for(j=1; j<=2; j++) {
      gene[genepool][3][1]=j; //agent vs agent's attributes stimulus
      if (j==2){
        for(o=1; o<=totattributes; p++){
          gene[genepool][4][1]=o;
          for (p=1; p<=population; p++){
            gene[genepool][5][1]=p;
            gene[genepool][6][1]=1;
              if (j==1) {
                for(g=1; g<=genes; g++){
                  gene[genepool][7][1]=g;
                  for(h=1; h<=genes; h++) {
                    gene[genepool][8][1]=h;
                    genepool++;
                  }
                }
              }
              if (j==2) {
                for(g=1; g<=totattributes; g++) {
                  gene[genepool][7][1]=g;
                  for(h=1; h<=genes; h++) {
                    gene[genepool][8][1]=h;
                    genepool++;
                  }
                }
              }
          }
        }
      }
      if (j==1){  //second one

          gene[genepool][4][1]=0;
          for (p=1; p<=population; p++){
            gene[genepool][5][1]=p;
            gene[genepool][6][1]=1;
              if (j==1) {
                for(g=1; g<=genes; g++){
                  gene[genepool][7][1]=g;
                  for(h=1; h<=genes; h++) {
                    gene[genepool][8][1]=h;
                    genepool++;
                  }
                }
              }
              if (j==2) {
                for(g=1; g<=totattributes; g++) {
                  gene[genepool][7][1]=g;
                  for(h=1; h<=genes; h++) {
                    gene[genepool][8][1]=h;
                    genepool++;
                  }
                }
              }
          }

      }
    }
  }


  for(i=1; i<=genepool; i++){
   for(j=1; j<=8; j++){
    fprintf(fp,"gene %d: {",i);
    fprintf(fp,"%d,", gene[i][j][1]);
    }
   fprintf(fp,"}");
  }



   fclose(fp);


}
  --------------B336608AE7D6093C8540CF1E--