This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Problem with malloc

Hi,

I need to create a dynamic array to send a different number of bytes of data using TWI.
Here is how I create an array:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void twi_Eeprom_Write(uint8_t *pBuffer, uint32_t WriteAddr, uint32_t NumBytesWritten)
{
ret_code_t err_code;
uint8_t SendAddr = 0;
uint8_t *reg;
reg = (uint8_t *)malloc((NumBytesWritten+2) * sizeof(uint8_t));
SendAddr = ((EEPROM_ADDR & 0xFC) | ((uint8_t)(WriteAddr >> 15) & 0x02));//A0 - write
reg[0] = (uint8_t)(((WriteAddr) & 0xFF00) >> 8);
reg[1] = (uint8_t)((WriteAddr) & 0x00FF);
for (uint8_t i = 0; i < NumBytesWritten; i++)
{
reg[i+2] = pBuffer[i];
}
err_code = nrf_drv_twi_tx(&m_twi_eeprom, SendAddr, reg, sizeof(reg), false);
APP_ERROR_CHECK(err_code);
free(reg);
TWI_Delay(10);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

When using the malloc function, I always get an array with a size of 4, regardless of the value of the X variable. I also tried to use the nrf_malloc function. The result is exactly the same.

Maybe there are ideas why he doesn't want to create an array of the required size.

Thank you in advance