Currently At: Programming Reference > C Reference > Standard C Memory > malloc()


#include <stdlib.h>

The function malloc() returns a pointer to a chunk of memory of size size, or NULL if there is an error. The memory pointed to will be on the heap, not the stack, so make sure to free it when you are done with it.

typedef struct data_type {
  int age;
  char name[20];
} data;

data *bob;
bob = (data*) malloc( sizeof(data) );
if( bob != NULL ) {
  bob->age = 22;
  strcpy( bob->name, "Robert" );
  printf( "%s is %d years old\n", bob->name, bob->age );
free( bob );

Related Topics:
free(), realloc(), calloc()

Web Standards & Support:

Link to and support Powered by LoadedWeb Web Hosting
Valid XHTML 1.0! Valid CSS! FireFox Extensions