Since the buffer is only allocating 1 char length...
No, it is not the case here.
Quoting from the C11
standard, chapter §7.22.3.4, The malloc
function
void *malloc(size_t size);
The malloc
function allocates space for an object whose size is specified by size
and
whose value is indeterminate.
So, in case of
foo = malloc(a); //yes, the cast is not required
a memory of size same as the value of a
will be allocated, considering malloc()
is successful.
Simply put, if I write a snippet like
int * p = malloc(10 * sizeof*p);
then, I can also write
for (int i = 0; i < 10, i++)
p[i] = i;
because, I have allocated the required memory for 10 int
s.
That said, please see this discussion on why not to cast the return value of malloc()
and family in C
..