Thanks for the reply @Kolban.
Sorry, This was meant to be a pointer variable to an unsigned 32 bit integer, not an actual 32 bit unsigned integer:
Code: Select all
uint32_t* foo=(uint32_t*)malloc(sizeof(uint32_t)*utf8string_length);
The pointer points to the start of a sequence of UTF-8 characters in heap memory. A UTF-8 code-point can occupy a value from U+0000 to U+10FFFF. Reference:
http://utf8-chartable.de/
This is thus beyond the range of a 16 bit unsigned integer, which would qualify a code point to be stored in a 32 bit unsigned integer.
As an example use case: The user enters the copyright symbol ©. This is send via BLE and this shows up as the UTF-8 encoded byte sequence 0xC2A9, There is a UTF-8 decoder running on the ESP32 that maps/decodes the UTF-8 encoded sequence 0xC2A9 to the UTF-8 codepoint U+00A9. This is then displayed on an display device.
In your example:
Code: Select all
for (int k=0; k<stringlength; k++)
{
char currentCharacter = foo[k];
}
Your sample code will work quite well with a sequence of elements a byte wide such as uint8_t,int8_t, char but will not work quite as well with element sizes greater than a byte such as uint16_t, uint32_t.
Here is a sample code one can try:
Code: Select all
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "stdio.h"
void app_main(void)
{
int length=4;
uint16_t *foo = (uint16_t*)malloc(sizeof(uint16_t)*length);
uint8_t *bar = (uint8_t*)malloc(length);
/* fill in buffers with deterministic data for testing*/
for(int i=0; i<length; i++)
{
*(foo + (sizeof(*foo) * i) ) = i;
*(bar + (sizeof(*bar) * i) ) = i;
}
printf("TEST 1 START: The element values of both buffers should match\n");
for(int j=0; j<length; j++)
{
printf("Character %d in uint16_t buffer expected: %d, got: %d\n", j, j, foo[j]);
printf("Character %d in uint8_t buffer expected: %d, got: %d\n", j, j, bar[j]);
}
printf("TEST 2 START: The element values of both buffers should match\n");
for(int k=0; k<length; k++)
{
printf("Character %d in uint16_t buffer expected: %d, got: %d\n", k, k, foo[sizeof(*foo)*k]);
printf("Character %d in uint8_t buffer expected: %d, got: %d\n", k, k, bar[sizeof(*bar)*k]);
}
while (1)
{
;
}
}
The result:
Code: Select all
TEST 1 START: The element values of both buffers should match
Character 0 in uint16_t buffer expected: 0, got: 0
Character 0 in uint8_t buffer expected: 0, got: 0
Character 1 in uint16_t buffer expected: 1, got: 16378
Character 1 in uint8_t buffer expected: 1, got: 1
Character 2 in uint16_t buffer expected: 2, got: 1
Character 2 in uint8_t buffer expected: 2, got: 2
Character 3 in uint16_t buffer expected: 3, got: 0
Character 3 in uint8_t buffer expected: 3, got: 3
TEST 2 START: The element values of both buffers should match
Character 0 in uint16_t buffer expected: 0, got: 0
Character 0 in uint8_t buffer expected: 0, got: 0
Character 1 in uint16_t buffer expected: 1, got: 1
Character 1 in uint8_t buffer expected: 1, got: 1
Character 2 in uint16_t buffer expected: 2, got: 2
Character 2 in uint8_t buffer expected: 2, got: 2
Character 3 in uint16_t buffer expected: 3, got: 3
Character 3 in uint8_t buffer expected: 3, got: 3
You can find the main.c file attached and replace it in the esp-idf-template/main/ folder to test.
Therefore one can see that for dynamically allocated arrays of byte width element one can access by the notation:
bar[j] as well as bar[sizeof(*bar)*k] from the sample code.
For dynamically allocated arrays, with element sizes greater than a byte, one can only access by:
foo[sizeof(*foo)*k] from the sample code.
My question is: Why cant I access an element for example in my uint16_t dynamically allocated array by foo[k]. Where k is the running loop variable. Is this specific to xtensa-gcc?
Thank you.