Local variables are allocated automatically
, rather than statically.
|All variables declared within a block of code are automatic by default, but this can be made explicit with the auto keyword.[note 1] An uninitialized automatic variable has an undefined value until it is assigned a valid value of its type.|
Static variables are, of course, allocated statically.
also provides the required storage.
To reiterate what Silvestar said...
The difference between approach in your code fragment and Silvestar's is that your's allocates memory on the stack, so you don't have to remember to free the memory, whereas with Silvestar's approach, you are allocating dynamic memory on the heap. This must be freed after use, in this case by calling
delete  buffer;
Note that for small array sizes, where you know the required size up front, and you only need the data for the lifetime of the function, stack based storage is better (e.g.
) Because there is less chance of leaking memory, and less for the heap allocator to do.
(GCC does allow you to use C99 variable array (e.g. char buffer[a]; where a variable of a non-const integral type), but this is not standard C++)
If you need a large amount of memory, you don't know the size up front, or you need to persist the memory between calls, then you need to use the heap (e.g. char *buffer = new char;)
What a "large array" is depends on context. If you're working with an embedded device, you might have to watch you stack usage very carefully. But with Windows programs, 1024 Bytes is not esp. big, as the default stack size in 1 MB (this memory does have to store the local variable for all functions in the current call stack, but that should be enough for quite a few locals.)