Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

malloc - Reserve memory space

&pagelevel(4)&pagelevel

Definition

#include <stdlib.h>

void *malloc(size_t n);

malloc allocates contiguous memory space of n bytes at execution time.

malloc is part of a C-specific memory management package which internally administers
memory areas that are requested and subsequently freed. Attempts are made to satisfy
new requests by first using areas that are already being managed and only then by the
operating system (cf. garbcoll function).

Return val.

Pointer to the new memory area


provided malloc was able to allocate new memory space. This pointer may
be used for any data type.

NULL pointer

if malloc was not able to provide the memory space, e.g. because the
memory space still available does not suffice for the request or because an
error occurred.

Notes

The new data area begins on a double word boundary.

The actual length of the data area amounts to:
the requested length n + 8 bytes for internal administrative data. If necessary, this sum is
rounded up to the next power of 2.

If malloc does not find enough memory space in the list of free blocks, the memalloc
function is internally called in order to obtain more memory space from the system.

You should use the sizeof function to ensure that you are requesting sufficient space for
a variable.

A serious disruption in working memory may be expected if the length of the memory area
provided is exceeded when writing.

If n has the value 0, malloc returns an unambiguous address which can also be transferred
to free.

Example 1

The following program fragment requests memory space for 30 integer elements.

#include <stdlib.h>
int *int_array;
    .
    .
    .
int_array = (int *)malloc(30 * sizeof(int));

Example 2

Dynamic reservation of memory space for data on second-hand cars:

#include <stdio.h>
#include <stdlib.h>
#define MAX 20;
struct car {
       char   *type;
       int    age;
       long   kilometers;
       char   inspect[6];
       int    cond;
       int    price;
       struct car *n;
       } *list;
int main(void)
{
  int mark;
  if((list = (struct car *)malloc(sizeof(*list))) == NULL)
    {
      printf("Memory space exhausted\n");
      exit(1);
    }
/* N.B. !! The preceding malloc call only provided space for a pointer     */ 
/* (4 bytes) for the member type. Space for the type identifier must still */
/* be provided.                                                            */
  if((list->type = (char *)calloc(1,20)) == NULL)
      exit(1);             /* error */
                           /* Input used car */
  scanf("%20s %d", list->type, &list->age);
  scanf("%d %6s %d %d", &list->kilometers, list->inspect, &list->cond,
    &list->price);
  list->n = NULL;
                           /* print input values */
  printf("%s\n%d\n", list->type, list->age);
  printf("%d\n%.6s\n%d\n%d", list->kilometers, list->inspect,
   list->cond, list->price);
                           /* free memory space */
  free(list);
  return 0;
}

See also

calloc, realloc, free, garbcoll, memalloc, memfree