Char array to int avoiding dynamic memory allocation

Hi,

I realise that the topic of char to int has been covered quite extensively in a number of posts but I can't see anything which discusses the conversion of a char array to an int avoiding dynamic memory allocation. To put my question into context I'm looking to run my code on a microcontroller and I'm worried about dynamic memory allocation causing problems. I realise methods such as atoi don't provide any error detection - but to my knowledge it doesn't dynamically allocate memory.

1
2
3
uint8_t data_uint8[3] = { '1', '9', '1 };
const char* data_ptr = reinterpret_cast<char*>(data_uint8);
result = atoi(data_ptr); 


Conversion without dynamic memory allocation.

1
2
3
4
uint8_t data_uint8[3] = { '1', '9', '1 };
const char* data_ptr = reinterpret_cast<char*>(data_uint8);
std::string str = std::string(data_ptr, 3);
result = std::stoi(str); 


Conversion with dynamic memory allocation (string).

Is there a way of doing the conversion which avoids dynamic memory allocation? Or in this instance is it best to use an unsafe function such as atoi?
Last edited on
Last edited on
Topic archived. No new replies allowed.