Post topics, source code that relate to the Arduino Platform

User avatar
By Josh Glendenning
#45954 I've been trying to get a sketch to work on my SparkFun ESP8266 thing using PlatformIO and the ESP8266 Arduino platform but I'm having a few issues.

Both `vasprintf` and `vsprintf` seem to be undefined- I understand `vasprintf` is a GNU extension and not ubiquitous, but I was wondering if this was some error on my part or if I'll have to implement this myself. The only thing I'm including in my source is #include "Arduino.h", but I have tried also including <stdio.h> to no avail.

Any help is appreciated.
User avatar
By martinayotte
#45996 looking at libc_replacements.c from ArduinoESP framework, you can see that there are few functions available :

Code: Select allsprintf()
snprintf()
vprintf()
vsnprintf()


Maybe one of those will suit your needs (probably the last one) ...
User avatar
By Josh Glendenning
#46020 Ah, I didn't realize I had to use vsnprintf. That seems to work fine for me (thanks a bunch!), sans one issue...

I'm trying to implement my own vasprintf which allocates a buffer large enough to hold the string, and I'm trying to use something like
Code: Select allint size = vsnprintf(0, 0, fmt, args);
to figure out how many bytes to allocate before doing another vsnprintf to the allocated buffer. Doing this crashes my ESP8266, though- am I doing something wrong? I've also tried passing in NULL for the first parameter.
User avatar
By martinayotte
#46036 If you are passing a NULL as the first argument, it is normal that you get a crash, it is the buffer pointer, and second argument is its size.

Code: Select allint ICACHE_RAM_ATTR vsnprintf(char * buffer, size_t size, const char * format, va_list arg) {
    return ets_vsnprintf(buffer, size, format, arg);
}


Just for quick test, try it that way :
Code: Select allchar buf[256];
int siz = vsnprintf(buf, sizeof(buf), fmt, args);