-->
Page 2 of 2

Re: Issue with vasprintf

PostPosted: Sat Apr 23, 2016 9:15 pm
by Josh Glendenning
Thanks again for the advice. I was able to get it working after fiddling with it for a while- using a valid char pointer instread of NULL prevents the craches, but apparently passing in a size of 0 causes issues as well. Using a size of 1 seems to work fine, however. So here's what I finally ended up with:

Code: Select all      int vasprintf(char** strp, const char* fmt, va_list ap) {
         va_list ap2;
         va_copy(ap2, ap);
         char tmp[1];
         int size = vsnprintf(tmp, 1, fmt, ap2);
         if (size <= 0) return size;
         va_end(ap2);
         size += 1;
         *strp = (char*)malloc(size * sizeof(char));
         return vsnprintf(*strp, size, fmt, ap);
      }

Re: Issue with vasprintf

PostPosted: Sun Apr 24, 2016 9:08 pm
by martinayotte
Glad that I was able to help you !
I don't understanding well about your implementation of vasprintf(), but since I've never use it ...

Re: Issue with vasprintf

PostPosted: Sun Apr 24, 2016 11:00 pm
by RichardS
How is the buffer ever free()'d after use???

I guess you can free(ptr) that it returns in the **strp??

RichardS