2 Replies Latest reply on May 7, 2019 9:09 PM by RaktimR_11

    Variable declarations mid-function cause problems

    NaFi_2915566

      Using Wiced Studio 6.2 for CYW43907 (43xxx_Wi-Fi project), I am having problems with mid-function variable declarations.  The following is a typical function that had all kinds of unexplained problems.  The declarations are all valid per C99 and the compiler does not give any warnings.  Has anybody else seen this?

       

      void createJson(char *strPtr, uint32_t strLen, uint8_t* input, uint32_t length)
      {
         // make sure we have enough data to even try
         if (length >= (DATA_READINGS_OFFSET+2))
         {
            // enough samples to process
            uint8_t *data = &input[DATA_READINGS_OFFSET];
            if (strPtr != NULL)
            {
               // get pointer to where we will write next data
               uint32_t index = strlen(strPtr);
               
               // check if adding to existing data
               if (index > 0)
               {
                  // there is already data, start with comma
                  index = strlen(strPtr);
                  snprintf(&strPtr[index], strLen-index, ",");
               }
               
               // add the header
               index = strlen(strPtr);
               snprintf(&strPtr[index], strLen-index, 
                               "{\"ts\":%llu,\"tick\":%lu,\"source\":%d,\"readings\":[",
                               EXT_TIMESTAMP(input),
                               EXT_TICK(input),
                               SOURCE_INDEX2(input));
               
               // add the data
               for(int i=0; i<numSamples; i++)
               {
                  // need to update index every pass, we do not know how many characters got written
                  index = strlen(strPtr);
                  // add comma if not first entry
                  if (i > 0)
                  {
                     index = strlen(strPtr);
                     snprintf(&strPtr[index], strLen-index, ",");
                  }
                  
                  // add entry
                  snprintf(&strPtr[index], strLen-index, "%d", s8_to_s16(data));
                  data += 2;
               }
               
               // wrap up
               index = strlen(strPtr);
               snprintf(&strPtr[index], strLen-index, "]}");
            }
         }
      }
      

       

      when we made the following change, everything worked fine.  the only difference is moving the variable declarations to the beginning of the function.

       

      void createJson(char *strPtr, uint32_t strLen, uint8_t* input, uint32_t length)
      {
         uint8_t *data;
         uint32_t index;
         int i;
      
         // make sure we have enough data to even try
         if (length >= (DATA_READINGS_OFFSET+2))
         {
            // enough samples to process
            data = &input[DATA_READINGS_OFFSET];
            if (strPtr != NULL)
            {
               // get pointer to where we will write next data
               index = strlen(strPtr);
               
               // check if adding to existing data
               if (index > 0)
               {
                  // there is already data, start with comma
                  index = strlen(strPtr);
                  snprintf(&strPtr[index], strLen-index, ",");
               }
               
               // add the header
               index = strlen(strPtr);
               snprintf(&strPtr[index], strLen-index, 
                               "{\"ts\":%llu,\"tick\":%lu,\"source\":%d,\"readings\":[",
                               EXT_TIMESTAMP(input),
                               EXT_TICK(input),
                               SOURCE_INDEX2(input));
               
               // add the data
               for(i=0; i<numSamples; i++)
               {
                  // need to update index every pass, we do not know how many characters got written
                  index = strlen(strPtr);
                  // add comma if not first entry
                  if (i > 0)
                  {
                     index = strlen(strPtr);
                     snprintf(&strPtr[index], strLen-index, ",");
                  }
                  
                  // add entry
                  snprintf(&strPtr[index], strLen-index, "%d", s8_to_s16(data));
                  data += 2;
               }
               
               // wrap up
               index = strlen(strPtr);
               snprintf(&strPtr[index], strLen-index, "]}");
            }
         }
      }