ESP32 at least has a handy buffer log function
struct {
String s13z = "thirteen char";
String s14z = "fourteen chars";
} someStrings;
Serial.printf("sizeof(2 String): %d\n", sizeof(someStrings));
log_buf_i((const uint8_t *)&someStrings, sizeof(someStrings));
Serial.printf("someStrings: %p\n", &someStrings);
Serial.printf(" s13z: %p c_str: %p\n", &someStrings.s13z, someStrings.s13z.c_str());
Serial.printf(" s14z: %p c_str: %p\n", &someStrings.s14z, someStrings.s14z.c_str());
which prints (scroll all the way to right if necessary)
sizeof(2 String): 32
/* 0x0000 */ 0x74, 0x68, 0x69, 0x72, 0x74, 0x65, 0x65, 0x6e, 0x20, 0x63, 0x68, 0x61, 0x72, 0x00, 0x00, 0x8d, // thirteen char...
/* 0x0010 */ 0xd0, 0x8e, 0xfb, 0x3f, 0x0f, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ...?............
someStrings: 0x3ffb224c
s13z: 0x3ffb224c c_str: 0x3ffb224c
s14z: 0x3ffb225c c_str: 0x3ffb8ed0
This is an implementation detail and could vary between boards and versions
- a
String
is 16 bytes
- (coincidentally the same bytes per line as
log_buf_?
)
- if the encoded characters plus terminating NUL are 14 bytes or less, they fit "inside" the
String
- along with the string length, with the high bit set
- otherwise the NUL-terminated characters are outside the
String
(heap? String pool?)
- with the
String
holding a pointer and the length