by Terr_ · 2y ago · 0 comments
Anecdote: I've got a couple languages and decades under my belt, and a very simple C/C++ Arduino project is making me doubt my sanity. Ex:
Serial.printf("%s", String("Hello world...")); // Emits 4 nonsense bytes... But shorter literals work fine.
________________________EDIT: To save readers some time--I didn't intend to nerd-snipe [0] y'all, honest!--the answer has been found, and it is probably:
Serial.printf("%s", String("Hello world...").c_str() );
The shorter payloads like "Hello World" happen to give the desired results only as an accident of optimization code.The C-language varargs concept and the C++ object model are not compatible. C++ provides varargs only for backwards compatibility with legacy C libraries. New APIs written in C++ should not be using it because passing a C++ object through it is undefined behaviour. No amount of rationalizing (as all the comments downthread are doing) is of any value: undefined behavior is undefined.
Clang at least errors out here.
C++ is a mess.
It sounds more like a crappy library designed by people who don't know their craft. It's easier to blame the tools.
I don't know that library, but have you tried this?
Serial.printf("%s", "Hello longer world");My default expectation is that it is a footgun that I do not understand.
The alternative is that I've stumbled across a very rare bug in some popular libraries, or else my hardware is cursed in a very specific and reproducible way.
Serial.printf("%s", String("Hello world...").c_str());
I actually don't see a "Serial.printf(...)" method in these docs [1], but I do see it here [2]. I'm not sure I'm looking at that right docs for your library though.[0] https://www.arduino.cc/reference/en/language/variables/data-...
[1] https://www.arduino.cc/reference/en/language/functions/commu...
[2] https://docs.particle.io/reference/device-os/api/serial/prin...
> I actually don't see a "Serial.printf(...)" method
I think it's coming from the ESP32-specific libraries. Some cursory searching didn't find the spot, but it may be some magical preprocessor directive stuff.
[0] https://github.com/espressif/arduino-esp32/blob/7a82915de215...
IIRC, some string implementations have separate implementations for very short strings vs. longer ones. Similar thing for vectors.
I also see some size-related logic in the (current?) String implementation [0] [1].
String is a class with no virtual methods, and its first data member is a pointer to the underlying buffer [2]. So if Stream.printf unintentionally type-puns String to char*, it might work out okay?
[0] https://github.com/arduino/ArduinoCore-API/blob/master/api/S...
[1] https://github.com/arduino/ArduinoCore-API/blob/master/api/S...
[2] https://github.com/arduino/ArduinoCore-API/blob/cb3ab4c90d71...
So basically the foot-gun is that smaller String()s appear to work "by accident".
[0] https://arduino.stackexchange.com/a/90332
[1] https://github.com/espressif/arduino-esp32/blob/7a82915de215...
In a C++ library you would probably make the format function type safe. But both Clang and gcc have annotations you can add to functions using format strings so they are type-checked
in my experience it's not to do with code. your serial/usb runs the bytes back through your debugger (typically IDE On the PC). Your IDE has config settings like baud rate +/- encoding that you have to config. If that IDE config is messed up (IDE Is presumably printing out your printf bytes over usb or whatever), then your output is bytes. Make sure your baud is correct.