# What is the return type here

**URL:** <https://forums.freertos.org/t/what-is-the-return-type-here/23795>\
**Category:** Kernel\
**Created:** [August 25, 2025, 5:01pm UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795 "2025-08-25T17:01:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 25, 2025, 5:01pm UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/1 "2025-08-25T17:01:14Z")

</div>

Hello,

Im busy with learning freertos and a YT course.  
But im stuck here :

```auto
pvPortMalloc(idx * sizeof(char));
        // Place the message on the heap  
        memcpy()

```

How can I know what the return type of pvPortMalloc is and what to add as first argument of memcpy ?

---

<div class="post-metadata">

**Author:** ![kstribrn](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/kstribrn/32/2085_2.png) [@kstribrn](https://forums.freertos.org/u/kstribrn)\
**Post date:** [August 25, 2025, 5:46pm UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/2 "2025-08-25T17:46:22Z")

</div>

The FreeRTOS coding style follows a naming convention to indicate the return type. You can read more about this [here](https://www.freertos.org/Documentation/02-Kernel/06-Coding-guidelines/02-FreeRTOS-Coding-Standard-and-Style-Guide). You can also inspect the code freely on Github to verify any functions you like - for example [pvPortMalloc](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/ccabdec2f8c2cb8c7740e018546a53a3e6e0ef22/include/portable.h#L189). In this case the `pv` prefix signifies a void pointer is returned.

For `memcpy` I suggest online documentation for the standard library functions, for example [this site](https://en.cppreference.com/w/cpp/string/byte/memcpy.html).

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 25, 2025, 6:04pm UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/3 "2025-08-25T18:04:48Z")

</div>

Thanks

It feels wierd to use a void pointer to a string,  
it is then not better to cast it to a char pointer ?

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [August 25, 2025, 7:59pm UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/4 "2025-08-25T19:59:24Z")

</div>

A pointer to void is a special “code” in C that it is a pointer that might point to anything, which could be a string. C will freely convert any other type of pointer to object to a pointer to void, or a pointer to void to any other type of pointer to object.

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 25, 2025, 8:08pm UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/5 "2025-08-25T20:08:51Z")

</div>

oke,

So im at a good path now to write a string to the heap ?

> **[RTOS\_lesson4 - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/440267157843445761)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [August 25, 2025, 8:58pm UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/6 "2025-08-25T20:58:52Z")

</div>

Look at your code. You first set idx to zero, then allocate space for idx characters, then copy idx characters.

What does that do?

I presume you will later do something with that buffer, so the fact that it is just leaked here isn’t a problem.

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 26, 2025, 5:18am UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/7 "2025-08-26T05:18:06Z")

</div>

oke

So idx and the pointer schould be global variables ??

or schould the code be like this ?

```auto
// make room on the heap for the message  
        void * ptr = pvPortMalloc(idx * sizeof(char));
        // Place the message on the heap  
        memcpy(ptr, buff, idx); 
        // set the flag 
        messageFlag = true;
        idx = 0 ; 
        memset(buff, 0, 20); 

```

---

<div class="post-metadata">

**Author:** ![Nikolay-Po](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/nikolay-po/32/5841_2.png) [@Nikolay-Po](https://forums.freertos.org/u/Nikolay-Po)\
**Post date:** [August 26, 2025, 7:06am UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/8 "2025-08-26T07:06:13Z")

</div>

It is not required to ptr to be global or something special. But it should be able to be passed to next code which will use a string from a heap and, finally, will free a heap when data are processed or are transferred.  
For example, you may pass ptr as an argument for a function which will use the data you’ve placed in a heap. Or you may make ptr global to use it as is. Also for next string processing, you may cast ptr to a string of chars (char\*)ptr to be precise and to ensure your data are handled as a string.  
Most important part here: don’t forget to free() the space you got from a heap. Else way a memory leak will occur.

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 26, 2025, 7:12am UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/9 "2025-08-26T07:12:56Z")

</div>

Thanks

I can cast the ptr to a char\* and to free the memory is exactly what this challenge is trying to learn me.

To declare memory and think of freeing memory

---

<div class="post-metadata">

**Author:** ![tony-josi-aws](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tony-josi-aws](https://forums.freertos.org/u/tony-josi-aws)\
**Post date:** [August 26, 2025, 7:13am UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/10 "2025-08-26T07:13:51Z")

</div>

The problem with the code you shared previously is that you set `idx` to zero and use it to calculate the number of bytes (`idx * sizeof(char)`) that need to be allocated via `pvPortMalloc`, which is zero.

Calling `pvPortMalloc`, to allocate zero bytes results in implementation-defined behavior; it can either return NULL or undefined behavior depending upon the implementation.

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 26, 2025, 7:17am UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/11 "2025-08-26T07:17:20Z")

</div>

Thanks

So this is the good way :

```auto
// my solution to lesson 4

// Learned so far
// - writing task
// - task scheduling
// - memory managament

// Challenge

// Write 2 tasks.

// task 1 : get input from serial monitor 
// after getting a enter , put it into heap memory 
// signal to task2 that there is something on the heap 

// task2 : read input from the heap if the task 1 signal that there is a message
// display it on the serial monitor 
// free the memory

#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0 ;
#else
static const BaseType_t app_cpu = 1;
#endif

// Pins
static const int led_pin = 2;
bool messageFlag = false; 
 char * message_ptr; 

// Our task is to read in a message 
void readMessageFromMonitor(void * parameter) {
  char ch;
  char buff[40];
  int idx = 0;
 
  memset(buff, 0, 20);
  while (1) {
    if (Serial.available() > 0) {
      ch = Serial.read();
       

      if (ch == '\n') {
      
        // clear buffer
       
        
        // make room on the heap for the message  
        message_ptr = (char *) pvPortMalloc(idx * sizeof(char));
        // Place the message on the heap  
        memcpy(message_ptr, buff, idx); 
        // set the flag 
        messageFlag = true;
        idx = 0 ; 
        memset(buff, 0, 20); 

      } else {
        if (idx < 39) {
           
          buff[idx] = ch;

                 
          idx++;
        }
      }
    }
  }
}

void setup() {
  pinMode(led_pin, OUTPUT);
  Serial.begin(9600);
  Serial.println("RW");

  // Task to run forever
  xTaskCreatePinnedToCore(
    readMessageFromMonitor, // Function to be called
    "Read Message", // name of the function
    1024, // stack size
    NULL, // parameter to pass to the function
    1, // priority
    NULL, // task handle
    app_cpu
  );
}

void loop() {
  
}

```

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [August 26, 2025, 9:20am UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/12 "2025-08-26T09:20:53Z")

</div>

I can spot the following issues:

1. The size of `buff` is 40 but the parameter to memset call is 20.
2. You want to add a check when `\n` is the first character (and therefore, `idx` is zero):

```auto
if ( (ch == '\n') && ( idx != 0 ) ) {

```

3. As others have pointed out, you are not freeing the memory you are allocating. You are probably yet to implement the second task described in comments:

```auto
// task2 : read input from the heap if the task 1 signal that there is a message
// display it on the serial monitor 
// free the memory

```

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 26, 2025, 11:26am UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/13 "2025-08-26T11:26:24Z")

</div>

Thanks for the feedback.

And it is correct that I did not write the second task.  
I want to be sure that the first task is good before moving on.

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 26, 2025, 4:21pm UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/14 "2025-08-26T16:21:19Z")

</div>

But do i not have to change the `\n` to a `\0` to make a proper string when store it on the heap and/or display it

And if so, how do I do that ?

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 26, 2025, 4:21pm UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/15 "2025-08-26T16:21:57Z")

</div>

> [@aggarg](#):
>
> `if ( (ch == '\n') && ( idx != 0 ) ) {`

and here I have to return it or do something else ?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [August 27, 2025, 5:29am UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/16 "2025-08-27T05:29:57Z")

</div>

> [@RoelofW](#):
>
> And if so, how do I do that ?

Here is the code:

```auto
/* Allocate one extra space for null terminator. */
message_ptr = (char *) pvPortMalloc( ( idx + 1 ) * sizeof(char));
.
.
.
message_ptr [idx] = '\0';

```

> [@RoelofW](#):
>
> and here I have to return it or do something else ?

That depends on what you want to do with the empty strings. Doing nothing is an option too:

```c
      if (ch == '\n') {
      
        if(idx != 0 )
        { // clear buffer
        
          
          // make room on the heap for the message  
          message_ptr = (char *) pvPortMalloc(idx * sizeof(char));
          // Place the message on the heap  
          memcpy(message_ptr, buff, idx); 
          // set the flag 
          messageFlag = true;
          idx = 0 ; 
          memset(buff, 0, 20); 
        }
        else
        {
          /* Ignore the empty buffer. */
        }
      } else {
        if (idx < 39) {
           
          buff[idx] = ch;

                 
          idx++;
        }
      }

```

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 27, 2025, 4:08pm UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/17 "2025-08-27T16:08:28Z")

</div>

oke

Could this be a good solution to the challenge

```auto
// my solution to lesson 4

// Learned so far
// - writing task
// - task scheduling
// - memory managament

// Challenge

// Write 2 tasks.

// task 1 : get input from serial monitor 
// after getting a enter , put it into heap memory 
// signal to task2 that there is something on the heap 

// task2 : read input from the heap if the task 1 signal that there is a message
// display it on the serial monitor 
// free the memory

#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0 ;
#else
static const BaseType_t app_cpu = 1;
#endif

// Pins
static const int led_pin = 2;
bool messageFlag = false; 
char * message_ptr; 

// Our task is to read in a message 
void readMessageFromMonitor(void * parameter) {
  char ch;
  char buff[40];
  int idx = 0;
  
  memset(buff, 0, 20);
  while (1) {
    if (Serial.available() > 0) {
      ch = Serial.read();
       

      if (ch == '\n') {
      
        if(idx != 0 )
        { // clear buffer
        
          
          // make room on the heap for the message  
          message_ptr = (char *) pvPortMalloc(idx * sizeof(char));
          // Place the message on the heap  
          memcpy(message_ptr, buff, idx); 
          // set the flag 
          messageFlag = true;
          idx = 0 ; 
          memset(buff, 0, 20); 
        }
        else
        {
          /* Ignore the empty buffer. */
        }
      } else {
        if (idx < 39) {
           
          buff[idx] = ch;

                 
          idx++;
        }
      }
    }
  }
}

void displayMessage() { 

  if (messageFlag) {
    Serial.print("Task2 reads :")
    Serial.println(message_ptr);
    vPortFree(message_ptr); 
    messageFlag = false ;  
  }
}

void setup() {
  pinMode(led_pin, OUTPUT);
  Serial.begin(9600);
  Serial.println("RW");

  // Task to run forever
  xTaskCreatePinnedToCore(
    readMessageFromMonitor, // Function to be called
    "Read Message", // name of the function
    1024, // stack size
    NULL, // parameter to pass to the function
    1, // priority
    NULL, // task handle
    app_cpu
  );

  xTaskCreatePinnedToCore(
    displayMessage, // Function to be called
    "Display Message", // name of the function
    1024, // stack size
    NULL, // parameter to pass to the function
    1, // priority
    NULL, // task handle
    app_cpu
  );
}

void loop() {
  
}

```

I cannot test it because I use wokike and that is one the whole day very very busy

---

<div class="post-metadata">

**Author:** ![RoelofW](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@RoelofW](https://forums.freertos.org/u/RoelofW)\
**Post date:** [August 27, 2025, 5:53pm UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/18 "2025-08-27T17:53:09Z")

</div>

Nope , it chrashing

```auto
RW

Task2 reads :Dit is een testxV­º?

E (1678) FreeRTOS: FreeRTOS Task "Display Message" should not return, Aborting now!

Guru Me

tbortn Errorcalled a0 PC 0x400 (c6f on core 1nel
exception). 
0x40

22fc:0x3fe: 440 0891088219f0x6ffb 46-CORR0PTEc

:0x3ffb
B80 tra0e: 06f00x32f99503
`` `

can anyone help me figure out why ?
```

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [August 28, 2025, 5:04am UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/19 "2025-08-28T05:04:43Z")

</div>

> [@RoelofW](#):
>
> `E (1678) FreeRTOS: FreeRTOS Task "Display Message" should not return, Aborting now!`

The error message tells you exactly why. FreeRTOS tasks cannot return and your `displayMessage` function returns. The signature of `displayMessage` is also wrong. I’d recommend first doing these tutorials to learn basics of FreeRTOS: [GitHub - FreeRTOS/Lab-Project-FreeRTOS-Tutorials: Tutorials to learn FreeRTOS Kernel.](https://github.com/FreeRTOS/Lab-Project-FreeRTOS-Tutorials).

---

<div class="post-metadata">

**Author:** ![kstribrn](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/kstribrn/32/2085_2.png) [@kstribrn](https://forums.freertos.org/u/kstribrn)\
**Post date:** [August 28, 2025, 4:48pm UTC](https://forums.freertos.org/t/what-is-the-return-type-here/23795/20 "2025-08-28T16:48:04Z")

</div>

One further observation - Task `readMessageFromMonitor` does not yield properly. This will result in task `displayMessage` never executing. The simplest (but not optimal) way to switch switch between the tasks would be to add a simple [taskYIELD](https://www.freertos.org/Documentation/02-Kernel/04-API-references/04-RTOS-kernel-control/00-Kernel-control#taskyield) call to both tasks. This would look like the following:

```auto

// Our task is to read in a message 
void readMessageFromMonitor(void * parameter) {
  char ch;
  char buff[40];
  int idx = 0;
  
  memset(buff, 0, 20);
  while (1) {
    if (Serial.available() > 0) {
      ch = Serial.read();
       

      if (ch == '\n') {
      
        if(idx != 0 )
        { // clear buffer
        
          buff[idx] = '\0';   
          // make room on the heap for the message  
          message_ptr = (char *) pvPortMalloc(idx * sizeof(char));
          // Place the message on the heap  
          memcpy(message_ptr, buff, idx); 
          // set the flag 
          messageFlag = true;
          idx = 0 ; 
          memset(buff, 0, 20); 
          taskYIELD(); // Yield so that the message can be displayed by the other task
        }
        else
        {
          /* Ignore the empty buffer. */
        }
      } else {
        if (idx < 39) {
           
          buff[idx] = ch;

                 
          idx++;
        }
      }
    }
  }
}

void displayMessage(void * parameter) { 
  while(1) {
  if (messageFlag) {
    Serial.print("Task2 reads :");
    Serial.println(message_ptr);
    vPortFree(message_ptr); 
    messageFlag = false ; 
    taskYIELD(); //Yield now that the message has been displayed
    
  }}
}

```

[Next page](https://forums.freertos.org/t/what-is-the-return-type-here/23795.md?page=2)
