Thread: An array element changes its value for no reason

  1. #1
    Registered User
    Join Date
    Jun 2022
    Posts
    2

    Question An array element changes its value for no reason

    Hello,
    I'm using C to write a user defined macro for the Ansys Fluent software. In a part of this macro, I read data from text files and store them in arrays , Here's the code:
    Code:
    DEFINE_EXECUTE_ON_LOADING(data_import,libname)  /*macro definition*/
    {
        FILE *fp ;
        size_t i, j, k ;                                                       /*Array indexes*/
        char *tok1, *tok2, row[1000] ;
        
        /*First file*/
        fp=fopen("RCT.csv","r");
        i = 0;
        while (1)                                           /*Loop to read each line of characters*/
        {
            fgets(row,50,fp);
            if (feof(fp))                                   /*Breaks the loop if ENDOFFILE=TRUE*/
            {
                break;
            }
            tok1=strtok(row,",");
            j=0;
            while (tok1 != NULL)                             /*Loop to read tokens of each line*/
            {
                blade_d[i][j] = strtod(tok1,NULL);
                tok1 = strtok(NULL,",");
                j++;
            }
            i++;
        }
    
    
        fclose(fp);
    
    
        Message("\n %f\n", blade_d[0][0]) ;     /*A command to display messages*/
    
    
        /*Second and third file*/
        fp=fopen("Data_C_l.txt","r");
        for (k = 0; k < 2; k++)
        {
            
            i=0;
            while (1)
            {
                fgets(row,1000,fp);
                if (feof(fp))
                {
                    break;
                }
                tok2=strtok(row," ");
                j=0;
                while (tok2 != NULL)
                {
                    airfoil_d[k][i][j] = strtod(tok2,NULL);
                    tok2 = strtok(NULL," ");
                    j++;
                }
                i++;  
            }
    
    
            fclose(fp);
            fp = fopen("Data_C_d.txt","r");
        }
    
    
        fclose(fp);
    
    
        Message("\n %f\n", blade_d[0][0]) ;
    }

    Now, the problem is that the first element of "blade_d" array changes value from 0.25 (the correct one) to 0.0 . And this happens somewhere between the two "Message" commands. Since I'm pretty new to C language, I'm quite confused about why this is happening.
    Any help is much appreciated.

    p.s. "blade_d" and "airfoil_d" arrays are defined as global variables above macro definition.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The easy guess is that you're writing to airfoil_d out of bounds, and airfoil_d happens to be adjacent to blade_d in memory such that this results in overwriting elements of blade_d.

    The general solution to this problem is to check that the array indices are valid before using them to access the arrays. Typically, this is done by comparing the index to the capacity (as in number of elements) of the given array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Consider some refactoring of the code.

    Code:
    #define B_MAX_ROWS  10
    #define B_MAX_COLS  10
    #define F_MAX_ROWS  10
    #define F_MAX_COLS  10
    
    double blade_d[B_MAX_ROWS][B_MAX_COLS];
    double airfoil_d[2][F_MAX_ROWS][F_MAX_COLS];
    
    void read_blade(const char *filename) {
        fp=fopen(filename,"r");
        if(fp) {
            char row[1000];
            for(int r = 0 ;
                r < B_MAX_ROWS && fgets(row,sizeof(row),fp) != NULL ; 
                r++ ) {
                char *tok;
                for(int c = 0, tok1 = strtok(row,",") ; 
                    c < B_MAX_COLS && tok != NULL ; 
                    c++, tok = strtok(NULL,",") ) {
                        blade_d[r][c] = strtod(tok,NULL);
                }
            }
            fclose(fp);
        }
    }
    
    void read_airfoil(const char *filename, int set) {
        fp=fopen(filename,"r");
        if(fp) {
            char row[1000];
            for(int r = 0 ;
                r < F_MAX_ROWS && fgets(row,sizeof(row),fp) != NULL ; 
                r++ ) {
                char *tok;
                for(int c = 0, tok1 = strtok(row," ") ; 
                    c < F_MAX_COLS && tok != NULL; 
                    c++, tok = strtok(NULL," ") ) {
                        airfoil_d[set][r][c] = strtod(tok,NULL);
                }
            }
            fclose(fp);
        }
    }
    
    DEFINE_EXECUTE_ON_LOADING(data_import,libname)  /*macro definition*/
    {
        /*First file*/
        read_blade("RCT.csv");
    
        Message("\n %f\n", blade_d[0][0]) ;     /*A command to display messages*/
     
        /*Second and third file*/
        read_airfoil("Data_C_l.txt",0);
        read_airfoil("Data_C_d.txt",1);
    
        Message("\n %f\n", blade_d[0][0]) ;
    }
    Points to note
    1. fgets returns a result, which does away with the messyness of checking feof
    2. You can have more than one variable in a for loop. This makes it easy to strtok your way through a string and keep track of how many values have been read.
    3. When reading from a file, error check and range check everything.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2022
    Posts
    2
    Thanks for your quick replies ,
    After reading your suggestions, I managed to solve this problem by editing the condition of the while loops to include a condition to check for the array range.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-16-2020, 07:40 AM
  2. how to compare element of array with other element
    By Djsarkar in forum C Programming
    Replies: 28
    Last Post: 08-04-2020, 03:59 PM
  3. Function writes to array for no reason
    By Finoli in forum C Programming
    Replies: 5
    Last Post: 01-06-2016, 02:55 PM
  4. Insert element after the last element in array in c
    By amaturequestion in forum C Programming
    Replies: 3
    Last Post: 04-09-2015, 08:29 AM
  5. array of ptrs vs constant ptr to an first element of array
    By monkey_c_monkey in forum C Programming
    Replies: 5
    Last Post: 08-30-2012, 11:39 PM

Tags for this Thread