Malloc:
In computing, malloc
is a function or subroutine for performing dynamic memory allocation in the C and C++ programming languages. malloc
is part of the standard library for both languages and is declared in the stdlib.h
header although it is also declared within the std
namespace via the C++’s cstdlib
header.function prototype of malloc is
void *malloc(size_t size);
Example: Here given a C program
#include <stdio.h>
#include <stdlib.h>
int
main ()
{
int
i,n;
char
* buffer;
printf (
"How long do you want the string? ");
scanf (
"%d", &i);
buffer = (
char*) malloc (i+1);
if
(buffer==NULL) exit (1);
for
(n=0; n<i; n++)
buffer[n]=rand()%26+
'a';
buffer[i]=
'\0';
printf (
"Random string: %s\n",buffer);
free (buffer);
return
0;
}
Thank you.