Wednesday, September 21, 2011

A sprintf with automatic memory allocation

Personally, I’m a sprintf lover. Not only in C, but in any program language that implements it. I use sprintf all the time, it’s extremely simple, clean and quick.

The sprintf version found on the C standard library requires a previously allocated memory space, where the formatted string will be placed after the operation. However, there are many times where you don’t know how long the result string can be, or you simply don’t want to waste space with a “worst case scenario” allocation. I used to face this a lot. There’s a GNU extension called asprintf, which you can use on Linux, but it doesn’t return the pointer, it returns the number of bytes – although it’s fine, it’s not exactly what I wanted.

So I decided to write my own version of a sprintf function which automatically finds out how much space is needed, allocates the memory and then returns the pointer, with the formatted string. Then I just have to free the pointer after use.

In order to accomplish this task, I used some least known functionalities from the C standard library, so it may be interesting to you to study how I did the job, so the technique can be applied when you need. Also, I found out that some of them behave differently on Linux and Win32. The idea is simple, though: find out how much memory is needed; allocate the memory; call sprintf; return the pointer. After all, I ended up with a valuable and handly function to work with.

This is the Linux version:
char* allocfmt(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	int len = vsnprintf(0, 0, fmt, ap);
	va_end(ap);

	va_start(ap, fmt);
	char *retbuf = malloc(sizeof(char) * (len + 1));
	vsprintf(retbuf, fmt, ap);
	va_end(ap);
	return retbuf;
}
And this is the Unicode-ready Win32 version:
#include <tchar.h>
#include <windows.h>
LPTSTR allocfmt(LPCTSTR fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	int len = _vsctprintf(fmt, ap);
	TCHAR *retbuf = malloc(sizeof(TCHAR) * (len + 1));
	_vsntprintf(retbuf, len, fmt, ap);
	va_end(ap);

	*(retbuf + len) = 0;
	return retbuf;
}

No comments: