I'm a bit lost why Option 1 doesn't work. In my understanding I should be able to write to a presized std::string
whatever I want as long as I don't overshoot the string size, but it doesn't work on godbolt and on my ESP32 it even causes a heap corruption downstream. What's the issue?
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
#define MACSTR_UPPER "%02X:%02X:%02X:%02X:%02X:%02X"
// Option 1: Doesn't work
// auto get_mac_address() -> std::string {
// const std::size_t s = 17;
// auto ret = std::string{ s, '\0' };
// uint8_t int_mac_addr[6] = { 0xFF, 0x16, 0x01, 0x25, 0xBB, 0x7A };
// std::snprintf(&ret.front(), s+1, MACSTR_UPPER, MAC2STR(int_mac_addr));
// return ret;
// }
// Option 2 Works? Why?
auto get_mac_address() -> std::string {
uint8_t int_mac_addr[6] = { 0xFF, 0x16, 0x01, 0x25, 0xBB, 0x7A };
char mac_str[18]; // 17 characters for MAC address + 1 for null terminator
std::snprintf(mac_str, sizeof(mac_str), MACSTR_UPPER, MAC2STR(int_mac_addr));
return std::string(mac_str);
}
int main() {
std::vector<std::string> vec = { std::string("my/mqtt/") + get_mac_address() + "/#",
std::string("other/mqtt/") + get_mac_address() + "/#"};
for (auto v : vec) {
std::cout << v << std::endl;
}
}
You are using the std::string
constructor taking a std::initializer_list<char>
so auto ret = std::string{ s, '\0' };
makes ret
a string with 2 characters only.
Use...
auto ret = std::string(s, '\0');
...instead.