:: Re: [DNG] "Common knowledge?"-quest…
Góra strony
Delete this message
Reply to this message
Autor: Rainer Weikusat
Data:  
Dla: Didier Kryn
CC: dng
Temat: Re: [DNG] "Common knowledge?"-question
Didier Kryn <kryn@???> writes:
> Le 24/01/2016 19:14, KatolaZ a écrit :
>>> Soo, the above is nearly the same as
>>> >
>>> > char buf[total];
>>> > p = buf;
>>> >
>>> >Why then use alloca()?
>>> >
>> Maybe because
>>      char buf[total];

>>
>> works only in ANSI C11. I still don't see the need for an internal
>> buffer to print out a formatted string, to be honest:)
>
>     The following works in plain old C:

>
> #include <stdio.h>
> #include <string.h>
> static void print_start(char const *name, char const *what)
> {
>      unsigned name_len, what_len, total;

>
>      name_len = strlen(name);
>      what_len = strlen(what);
>      total = name_len + what_len + 3;
>      {
>        char buf[total], *p=buf;


It works if the compiler supports the VLA (variable-length array)
feature introduced with C99. But besides apparently aesthetically
pleasing to certain people, it doesn't offer anything which alloca
doesn't offer as well and doesn't offer some things the latter does, eg,
allocating structures which will end up being linked together via
pointers.