malloc(3C)
_________________________________________________________________
malloc function
Allocate blocks of memory.
_________________________________________________________________
Calling Sequence
char *ptr, *malloc();
int size;
ptr = malloc(size);
where size is the size, in bytes, of an object.
Description
Use the malloc function to allocate blocks of memory without
having them zeroed out. The include file stdio.h defines this
entry.
Since hardware representations for pointers to characters differ
from pointers to other types, use a cast operation to convert the
result to a pointer of the appropriate type, such as
ip = (int *)malloc(sizeof(int));
With the malloc function, any pointer returned is a word-aligned
byte pointer. It can be used, following the appropriate
conversion, as a pointer to any item.
Returns
The malloc function returns 0 if it allocates no memory.
Otherwise, it returns a pointer to the first element of the
memory area.
Related Functions
See also the alloc, calloc, realloc, and free functions.
Example
/* Program test for the malloc() function */
#include <stdio.h>
char *ptr, *malloc();
DG/UX 4.00 Page 1
Licensed material--property of copyright holder(s)
malloc(3C)
int size;
main(argc, argv)
int argc;
char *argv[];
{
size = atoi(argv[1]);
ptr = malloc(size);
printf("size = %d bytes in the block %o.\n", size, ptr);
}
A call to the program test with an argument of 512 generates the
output
size = 512 bytes in the block 34003702770.
(The address will vary.)
DG/UX 4.00 Page 2
Licensed material--property of copyright holder(s)