Skip to main content

# 12.3: Allocating Memory

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$

( \newcommand{\kernel}{\mathrm{null}\,}\) $$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\id}{\mathrm{id}}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\kernel}{\mathrm{null}\,}$$

$$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$

$$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$

$$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

To use the memory routines, include the stdlib.h header in your code and be sure to link with the standard library. There are two main memory allocation functions. They are malloc() and calloc(). Here are their prototypes:

void * malloc( unsigned int size );
void * calloc( unsigned int num_item, unsigned int item_size );


malloc() takes a single argument: The number of bytes that you wish to allocate from the free pool. calloc() takes two arguments: The number of items that you want to fit into the memory block and their size in bytes. Basically, calloc() just calls malloc() after multiplying the two arguments together. It is used for convenience. Both functions return a pointer to a type void. What is this? A void pointer can be thought of as a generic, one-size-fits-all pointer. It prevents possible type size clashes. You can assign a void pointer to another type of pointer and not get a type mismatch. If the memory request cannot be made (not enough memory) then the functions will return NULL. Always check for the NULL return! Never assume that the allocation will work!

If you want to obtain space for 100 bytes, you’d do something like this:

char *cp;

cp = malloc( 100 );
if( cp )
{
/* memory allocated, do stuff... */
} else
{
/* not allocated, warn user and fail gracefully... */
}


If you need space for 200 doubles, you’d do something like this:

double *dp;

if( dp = calloc( 200, sizeof(double) ) ) /* assign and if test in 1 */
{
/* memory allocated, do stuff... */
}
else {
/* not allocated, warn user and fail gracefully... */
}


Note the use of the sizeof() operator above. If you had a structure and needed to create one (for example, to add to a linked list), you might do this:

struct foo *fp;

if( fp = calloc( 1, sizeof(struct foo) ) )
{
/* remainder as above ... */


This page titled 12.3: Allocating Memory is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by James M. Fiore via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

• Was this article helpful?