Brandon Jerz
Brandon Jerz

Reputation: 153

Arrays being randomly overwritten in C program. Probably simple solution

I have 4 functions that run through a text file and store the values to their designated arrays decalred at the beginning of the problem. The issue i'm having is that when I update one of the arrays the others all are never updated or are reset. So for example if I run the getkosDS function it will update the array and print out the value i am looking for. The other arrays will contain zero. However if I comment out the getkosDS function the next array (nipsDS) will update and not be zero!!! I am very confused.

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int kosND;
int kosNW;
int kosNZC;
int enronND;
int enronNW;
int enronNZC;
int nipsND;
int nipsNW;
int nipsNZC;
int nytND;
int nytNW;
int nytNZC;
int world_size;
int my_rank;
int kosDS[3430];
int nipsDS[1500];
int enronDS[39861];
int nytDS[300000];

void getKOS(){
    int i;
    FILE *MyFile;
    char line[25];
    MyFile=fopen("/home/mcconnel/BagOfWords/docword.kos.txt","r");
    for(i=0; i<5; i++){
        fscanf(MyFile, "%s", line);
        if(i == 0){
                kosND = atoi(line);
        }
        if(i == 1){
                kosNW = atoi(line);
        }
        if(i == 2){
                kosNZC = atoi(line);
        }
    }
    fclose(MyFile);
}

void getEnron(){
    int i;
    FILE *MyFile;
    char line[25];
    MyFile=fopen("/home/mcconnel/BagOfWords/docword.enron.txt","r");
    for(i=0; i<5; i++){
        fscanf(MyFile, "%s", line);
        if(i == 0){
                enronND = atoi(line);
        }
        if(i == 1){
                enronNW = atoi(line);
        }
        if(i == 2){
                enronNZC = atoi(line);
        }
    }
}

void getNips(){
    int i;
    FILE *MyFile;
    char line[25];
    MyFile=fopen("/home/mcconnel/BagOfWords/docword.nips.txt","r");
    for(i=0; i<5; i++){
        fscanf(MyFile, "%s", line);
        if(i == 0){
                nipsND = atoi(line);
        }
        if(i == 1){
                nipsNW = atoi(line);
        }
        if(i == 2){
                nipsNZC = atoi(line);
        }
    }
    fclose(MyFile);
}

void getNYT(){
    int i;
    FILE *MyFile;
    char line[25];
    MyFile=fopen("/home/mcconnel/BagOfWords/docword.nytimes.txt","r");
    for(i=0; i<5; i++){
        fscanf(MyFile, "%s", line);
        if(i == 0){
                nytND = atoi(line);
        }
        if(i == 1){
                nytNW = atoi(line);
        }
        if(i == 2){
                nytNZC = atoi(line);
        }
    }
    fclose(MyFile);
}

void getKosDS(){
    int i;
    int z;
    FILE *MyFile;
    char line[25];
    MyFile=fopen("/home/mcconnel/BagOfWords/docstart.kos.txt","r");
    for(i = 0; i<3430; i++){
        fscanf(MyFile, "%s", line);
        if(i != 0 && i % 2 == 1){
                kosDS[z] = atoi(line);
                z++;
        }
    }
    fclose(MyFile);
}


int getNipsDS(){
    int i;
    int z;
    FILE *MyFile;
    char line[25];
    MyFile=fopen("/home/mcconnel/BagOfWords/docstart.nips.txt","r");
    for(i = 0; i<1500; i++){
        fscanf(MyFile, "%s", line);
        if(i != 0 && i % 2 == 1){
                nipsDS[z] = atoi(line);
                z++;
        }
    }
    fclose(MyFile);
}


int main(int argc, char** argv)
{  MPI_Init(NULL,NULL);
   MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  // printf("\n%d", world_size);
   MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  // printf("\n%d", my_rank);
   if(my_rank == 0){
        getKOS();
        getEnron();
        getNips();
        getNYT();
        getKosDS();
      //  getEnronDS();
      //  getNytDS();
        printf("\ngetting complete\n");
        printf("KOS location %d\n", kosDS[5]);
      //  printf("Enron location %d\n", enronDS[200]);
        printf("Nips location %d\n", nipsDS[500]);
      //  printf("NYT location %d\n", nytDS[10000]);

   }
   else{

        printf("\n%d \n", my_rank);

   }
   MPI_Finalize();
}

this is the result

KOS location 4875 Nips location 0

however if I were to comment out getKosDS the result is

KOS location 0 Nips location 28340

Craziness, if you can't tell I'm new to C

Upvotes: 0

Views: 138

Answers (1)

Eugene Sh.
Eugene Sh.

Reputation: 18299

In getKosDS the z is uninitialized, so by writing to nipsDS[z] you are actually writing into a random memory location, invoking an undefined behavior.

Update: The same problem is in the getNipsDS function.

Upvotes: 4

Related Questions