How can I dynamicly allocate the site of an array (int8_t)

Hello,

I a trying to limit the amount of data sent to the server with LoRa.

I declare a variable this way:

uint8_t payload[51];
memset(payload,'\0',payloadSize);

But when I have some unactive sensor, I only use 24 space

In toher word, when I merge the data into payload and I print the length

       Si.sprint(F("Payload len: "),2); Si.sprintln(pl,2);
        int ps = sizeof(payload);
        Si.sprint(F("Payload size: "),2); Si.sprintln(ps,2);

I got this value

Payload len: 24
Payload size: 51

any the payload value is:

61 32 38 62 39 36 34 63 34 36 64 32 35 65 31 30 30 68 30 6B 34 32 39 2C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

As you can see, there is a lot of 00, I would like to eliminate.

My idea is to realocate the size of payload to 24 (or to the result of strlen(payload);

I guess we can reallocate the size of a int8_t? that’s right?

Orwould it better to copy payload to a new variable

int8_t new_payload[strlen(payload)];

new_payload = payload;

Smaller packs fit in the big buffer, just send less bytes.

You gave only snippets of the problem, so I can not tell you more.

Dear Whandall

Smaller packs fit in the big buffer, just send less bytes.

It's what I am doing, but as payload is initialized with 51
and then I clear is with 0

memset(payload,'\0',payloadSize);

the zeros you can see here

61 32 38 62 39 36 34 63 34 36 64 32 35 65 31 30 30 68 30 6B 34 32 39 2C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

are not values that I sent.
The values that I sent is

61 32 38 62 39 36 34 63 34 36 64 32 35 65 31 30 30 68 30 6B 34 32 39 2C

and the last 2C, could be removed

try to only print the amount of data received, not the whole buffer

For text, use strlen() instead of sizeof()