I have an error when try output a lot of variable via printf.
unsigned int vmID = 11;
char vm_title[512] = "title-123qwe";
unsigned int vm_cpu = 2;
unsigned int vm_ram = 12;
char vm_serveces[2048] = "123456789qweertrty";
char vm_notes[2048] = "wertyuio2345678";
unsigned int vm_vc = 3;
unsigned int vm_dc = 12;
unsigned int vm_cl = 6;
unsigned int vm_pl = 32;
unsigned int vm_os = 0;
printf("name='%s', cpu='%d', ram='%d', services='%s', notes='%s', vcname='%d', dcname='%d', clname='%d', poolname='%d', os='%d' id='%d';", vm_title, vm_cpu, vm_ram, vm_serveces, vm_notes, vm_vc, vm_dc, vm_cl, vm_pl, vm_os, vmID);
But But individually or in small groups work right:
printf("name='%s,", vm_title);
printf("cpu='%d',", vm_cpu);
printf("ram='%d',", vm_ram);
printf("services='%s',", vm_serveces);
printf("notes='%s',", vm_notes);
printf("vc='%d',", vm_vc);
printf("dc='%d',", vm_dc);
printf("cl='%d',", vm_cl);
printf("pl='%d',", vm_pl);
printf("os='%d';", vm_os);
I wait: "name='title-123qwe', cpu='2', ram='12', services='123456789qweertrty', notes='wertyuio2345678', vcname='3', dcname='12', clname='6', poolname='32', os='0' id='11', name='title-123qwe';"
P.S. For my task I use construction such as (not work too):
int len = snprintf(NULL,0, "xxx", yyy);
char *somevar = malloc(len + 1);
sprintf(somevar, "xxx", yyy);
What wrong? Is it possible to use a single call or will have to glue variable in a few hits?
Hm. I create a new file:
#include <stdlib.h>
#include <string.h>
int main () {
unsigned int vmID = 11;
char vm_title[512] = "title-123qwe";
unsigned int vm_cpu = 2;
unsigned int vm_ram = 12;
char vm_serveces[2048] = "123456789qweertrty";
char vm_notes[2048] = "wertyuio2345678";
unsigned int vm_vc = 3;
unsigned int vm_dc = 12;
unsigned int vm_cl = 6;
unsigned int vm_pl = 32;
unsigned int vm_os = 0;
// vc_ram = vm_pl + vm_vc;
printf("name='%s', cpu='%d', ram='%d', services='%s', notes='%s', vcname='%d', dcname='%d', clname='%d', poolname='%d', os='%d' id='%d';", vm_title, vm_cpu, vm_ram, vm_serveces, vm_notes, vm_vc, vm_dc, vm_cl, vm_pl, vm_os, vmID);
printf("name='%s,", vm_title);
printf("cpu='%d',", vm_cpu);
printf("ram='%d',", vm_ram);
printf("services='%s',", vm_serveces);
printf("notes='%s',", vm_notes);
printf("vc='%d',", vm_vc);
printf("dc='%d',", vm_dc);
printf("cl='%d',", vm_cl);
printf("pl='%d',", vm_pl);
printf("os='%d';", vm_os);
return 0;
}
It's work fine.
BUT if I uncomment string vc_ram = vm_pl + vm_vc; = segfault
Any manipulations with variables and I have fail.