CodeCogs
HomeForumsCart
Login



Forgotten Password?

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingCStdlib.h

malloc

Memory allocation

Interface

#include <stdlib.h>
void* malloc (size_t size)
void* calloc (size_t count, size_t size)
void* valloc (size_t size)
void* realloc (void *ptr, size_t size)
void* reallocf (void *ptr, size_t size)
void free (void *ptr)
size_t malloc_size (void *ptr)
size_t malloc_good_size (size_t size)

Description

The malloc, calloc, valloc, realloc and reallocf functions allocate memory. The allocated memory is aligned such that it can be used for any data type, including AltiVec-related types. The free function frees allocations that were created via the preceding allocation functions. The malloc_size and malloc_good_size functions provide information related to the amount of padding space at the end of allocations.

The malloc function allocates size bytes of memory and returns a pointer to the allocated memory. malloc returns a NULL pointer if there is an error.

The calloc function contiguously allocates enough space for count objects that are size bytes of memory each and returns a pointer to the allocated memory. The allocated memory is filled with bytes of value zero. calloc returns a NULL pointer if there is an error.

The valloc function allocates size bytes of memory and returns a pointer to the allocated memory. The allocated memory is aligned on a page boundary. valloc returns a NULL pointer if there is an error.

The realloc function tries to change the size of the allocation pointed to by ptr to size, and return ptr. If there is not enough room to enlarge the memory allocation pointed to by ptr, realloc creates a new allocation, copies as much of the old data pointed to by ptr as will fit to the new allocation, frees the old allocation, and returns a pointer to the allocated memory. realloc returns a NULL pointer if there is an error, and the allocation pointed to by ptr is still valid.

The reallocf function is identical to the realloc function, except that it will free the passed pointer when the requested memory cannot be allocated. This is a FreeBSD specific API designed to ease the problems with traditional coding styles for realloc causing memory leaks in libraries.

The free function deallocates the memory allocation pointed to by ptr.

The malloc_size function returns the size of the memory block that backs the allocation pointed to by ptr. The memory block size is always at least as large as the allocation it backs, and may be larger.

The malloc_good_size function rounds size up to a value that the allocator implementation can allocate without adding any padding and returns that rounded up value.

Example 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
  // allocate memory for the string
  char *string = (char *) malloc(100*sizeof(char));
 
  // write some data to the string
  strcpy(string, "Hello world!");
 
  // display the string
  printf("%s\n", string);
 
  // remove the string from memory
  free(string);
 
  return 0;
}

Output:
Hello world!

Return Values

If successful, the malloc, calloc and valloc functions return a pointer to allocated memory. If there is an error, they return a NULL pointer and set errno to ENOMEM.

If successful, the realloc and reallocf functions return a pointer to allocated memory. If there is an error, it returns a NULL pointer and sets errno to ENOMEM.

The free function does not return a value.

Last Modified: 18 Dec 11 @ 14:46     Page Rendered: 2022-03-14 05:08:42