Variable declarations mid-function cause problems

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
NaFi_2915566
Level 3
Level 3
First like received First like given

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, "]}");

      }

   }

}

0 Likes
1 Solution
RaktimR_11
Moderator
Moderator
Moderator
500 replies posted 250 replies posted 100 replies posted

By default WICED Studio support C11 standard. But if you want to use C99 standard, you can just modify the COMPILER_SPECIFIC_STANDARD_CFLAGS in wiced_toolchain_ARM_GNU.mk as -std=gnu99 and in the same file modify COMPILER_SPECIFIC_PEDANTIC_CFLAGS to -std=c99. Could you please incorporate these changes and see if you are getting any error?

View solution in original post

0 Likes
2 Replies
Zhengbao_Zhang
Moderator
Moderator
Moderator
250 sign-ins First comment on KBA 10 questions asked

Hello:

    for(int i=0; i<numSamples; i++) 

Do you mean this declaration brought the unexplained problems ?

Actually, I found a  lot of this kind of declarations in the code.

0 Likes
RaktimR_11
Moderator
Moderator
Moderator
500 replies posted 250 replies posted 100 replies posted

By default WICED Studio support C11 standard. But if you want to use C99 standard, you can just modify the COMPILER_SPECIFIC_STANDARD_CFLAGS in wiced_toolchain_ARM_GNU.mk as -std=gnu99 and in the same file modify COMPILER_SPECIFIC_PEDANTIC_CFLAGS to -std=c99. Could you please incorporate these changes and see if you are getting any error?

0 Likes