#include #include #define ssprintf(...) \ ({ int _ss_size = snprintf(0, 0, ##__VA_ARGS__); \ char *_ss_ret = __builtin_alloca(_ss_size+1); \ snprintf(_ss_ret, _ss_size+1, ##__VA_ARGS__); \ _ss_ret; }) int main() { char *tmp[5]; tmp[0] = ssprintf("This text is dynamically allocated"); tmp[1] = ssprintf("on the stack frame of main() and doesn't"); tmp[2] = ssprintf("need to be freed explicitely. It is"); tmp[3] = ssprintf("automatically freed when the function returns."); tmp[4] = ssprintf("%s\n%s\n%s\n%s\n", tmp[0], tmp[1], tmp[2], tmp[3]); printf("\nt: %p\t0: %p\t1: %p\n2: %p\t3: %p\t4: %p\n\n%s\n", tmp, tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[4]); return 0; }