Menu

Array index out of bounds

Joe
2023-09-26
2023-09-27
  • Joe

    Joe - 2023-09-26

    Hi,

    I get the error "Array index out of bounds, cannot determine dbIdent is less than 10".

    This is my code:

      for(uint8_t dbIdent = 0; 10 > dbIdent; dbIdent++)
      {
        if(databaseEntry[dbIdent].testVar == testVar)
        {   
            // Do something
        }
      }
    

    But in the for loop it checks if dbIdent is less than 10 anyway?

    Any help with this is appreciated.
    Thanks.

     
  • CHR

    CHR - 2023-09-26

    Can you show a minimal example that reproduces the problem? How is databaseEntrydeclared?

     
  • Joe

    Joe - 2023-09-26
    typedef struct
    {
        uint8_t testVar;
    }databaseEntry_t;
    
    static databaseEntry_t databaseEntry[10] = {0};
    
     

    Last edit: Joe 2023-09-26
  • CHR

    CHR - 2023-09-26

    Which version do you use?
    No warning:

    typedef struct
    {
        uint8_t testVar;
    }databaseEntry_t;
    
    static databaseEntry_t databaseEntry[10] = {0};
    
    void f(uint8_t testVar) {
    
      for (uint8_t dbIdent = 0; 10 > dbIdent; dbIdent++)
      {
        if(databaseEntry[dbIdent].testVar == testVar)
        {   
            // Do something
        }
      }
    }
    
     
  • Joe

    Joe - 2023-09-27

    Okay thank you.
    Currently I'm using 2.7 still I believe the newer versions got rid of bug hunting?
    Which version did you use?

    Thanks.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.