-
Case 1: In
char Buffer[MAX_BUF];
Buffer
is an array of sizeMAX_BUF
. The allocation technique is called VLA. -
Case 2: In
const int MAX_BUF = 1000; char* Buffer = malloc(MAX_BUF);
Buffer
is a pointer which is allocated a memory of sizeMAX_BUF
which is1000
.
and, an array is not the same as a pointer, and C-FAQ has a Very Good collection detailing the reasons.
The major difference, in terms of usability and behaviour are:
- (1) is on stack, usually Note, while (2) is on heap, always.
- (1) has fixed size once allocated, (2) can be resized.
- (1) is allocated when the enclosing function is called and has the block scope OTOH, (2) is allocated memory dynamically, at runtime and the returned memory has a lifetime which extends from the allocation until the deallocation.
- (1) allocated memory need not be managed by programmer, while in (2) all
malloc()
d memory should befree()
d. [Courtesy: Giorgi]
Note: Wiki
For example, the GNU C Compiler allocates memory for VLAs on the stack.