Problem whith xQueueSend and array

ignvirg wrote on Sunday, October 26, 2008:

Hello,

I’m a newbie in FreeRTOS and C. While trying out I had some trouble in understanding why source code A does work, but B and C do not work properly.

/*** simplified functions ***/
void vStringOutput (portCHAR *pcSomeString);

void vFunctionWithQueueReceive(void)
{
     portCHAR *pcReceivedString;
     xQueueHandle xQueue;
    
     xQueue = xQueueCreate((20, sizeof(portCHAR *));
    
     if(xQueue != 0)
     {
          if(xQueueReceive(xQueue, &pcReceivedString, 10))
          {
               xStringOutput(pcReceivedString);
          }
     }
}

A: This works perfectly
void vFunctionWithQueueSend(void)
{
     …
     portChar *pcAString = "abcdefg";
     …
     xQueueSend(xQueue, &pcAString1, 10);
     …
}

B: This puts out wierd characters
void vFunctionWithQueueSend(void)
{
     …
     portChar cAnArray[] = "abcdefg";
     …
     xQueueSend(xQueue, &cAnArray, 10);
     …
}

C: This puts out wierd characters
void vFunctionWithQueueSend(void)
{
     …
     xQueueSend(xQueue, (void *)"abcdefg", 10);
     …
}

How can I manage to put a char array in a Queue?

Thank you in advance.

Dennis 

rtel wrote on Sunday, October 26, 2008:

> A: This works perfectly
> void vFunctionWithQueueSend(void)
> {
>      …
>      portChar *pcAString = "abcdefg";
>      …
>      xQueueSend(xQueue, &pcAString1, 10);
>      …
> }

pcAString is a pointer to a const string, which is probably stored in Flash.  The pointer contains the address of data that is in flash and is always there.

> B: This puts out wierd characters
> void vFunctionWithQueueSend(void)
> {
>      …
>      portChar cAnArray[] = "abcdefg";
>      …
>      xQueueSend(xQueue, &cAnArray, 10);
>      …
> }

You have declared an array as a local (stack) variable within vFunctionWithQueueSend().  Once vFunctionWithQueueSend() exits the variable will no longer exist and its value in RAM will probably be overwritten during the next function call.

>
> C: This puts out wierd characters
> void vFunctionWithQueueSend(void)
> {
>      …
>      xQueueSend(xQueue, (void *)"abcdefg", 10);
>      …
> }

Here you are posting a character pointer (char *), but your queue receive function is expecting a pointer to a character pointer (char **).

These statements are just general C pointers, the same would be true if you were using FreeRTOS.org or not.

Regards.

ignvirg wrote on Sunday, October 26, 2008:

Thank you very much! I thought that my problem has to do with FreeRTOS, but now I understand that it is an C issue. I have to read more about pointer… :slight_smile:

With best Regards
Dennis 

ignvirg wrote on Sunday, October 26, 2008:

After thinking to understand the problem and after many trials I’m not able to pass the array in the queue like intendet in B.
If possible i want to avoid global arrays, so I have to use a local one.
I tryed to allocate dynamic memory for a pointer and copied each array element to the allocated memory, but it didn’t work.
Without using a queue I would try to call the function tranferring a pointer and work with a reference or maybe I would try a function which returns a pointer.
Since I have to use xQueueSend() and I read that the items are passed by copy, I have no ideas how to continue.

Are there any hints?     

What possibilities do I have to send the array from example B threadsave to the Queue?

Regards

Dennis