What is the difference between this:
somefunction() { ... char *output; output = (char *) malloc((len * 2) + 1); ... }
and this:
somefunction() { ... char output[(len * 2) + 1]; ... }
When is another suitable than the other?
Thank you all for your answers. here is the summary:
Corrections are welcome.
Here is an explanation of the difference between a heap and a stack:What and where is the stack and the heap?
, , , . , malloc.
, , , , malloc go. > 16 , , , . kb/mb - .
. , , . , , .
. , . , .
. . , function(). - , , , . , , , ..
. , ++-, new malloc(),
output = new char[len+1];
len * 2 + 1 ? , - , , Unicode . unicode, , , char - , 8- . , , .
:
: , :
++, C, ++. C (99).
, "len" , .
malloc() ( ++ 'new') , , () ( "", "" ) ().
, . , , .
, ( ++ RAII, ), , .
, "", :
static char output[(len * 2) + 1];
, , , .
Finally, do not use malloc in C ++ unless you have a really good reason (i.e. realloc). Instead, use the "new" and the accompanying "delete".