<reponame>thoth-tech/programming-arcana
\clearpage
\subsection{Allocating Memory} % (fold)
\label{sub:allocating_memory}

With dynamic memory management, one of the tasks you can perform is to request space from the heap. With this request the Operating System will locate available space and allocate this to you for use in your code. The only thing the Operating System really needs to know is how much space do you require? It can then search for a free space of that size, allocate this to you, and then give you a pointer to this newly allocated area of memory.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.75\textwidth]{./topics/dynamic-memory/diagrams/AllocateMemory-Overview} 
   \caption{When requesting a memory allocation you need to specify the size you want}
   \label{fig:allocate-memory-overview}
\end{figure}

\mynote{
\begin{itemize}
  \item Allocating memory is an \textbf{action} you can perform by calling appropriate Functions or Procedures offered to you by the Programming Language.
  \item Your request for memory must include an indicating of the amount of memory that you require.
  \item You also need to have a pointer that will refer to the memory you are allocated.
  \item It is possible that your request will be denied, this occurs when the computer has run out of memory to allocate.
\end{itemize}
}


\clearpage
\subsubsection{Explicitly allocating memory for a single value} % (fold)
\label{ssub:explicitly_allocating_memory}

If you want to store a single value on the heap you can ask to be allocated enough space for a single value. \fref{fig:allocate-memory} shows a Pointer (\texttt{p}) that points to an Integer value. If you want this value to be on the Heap you can ask to be allocated enough space to store an integer (4 bytes). The Operating System will then allocate you 4 bytes of space from the Heap, and give you the address of this space.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.85\textwidth]{./topics/dynamic-memory/diagrams/AllocateMemory} 
   \caption{You can ask to be allocated enough space to store one value}
   \label{fig:allocate-memory}
\end{figure}


\csection{
In C the \texttt{malloc} function from \texttt{stdlib.h} is used to allocate memory. With \texttt{malloc} you must specify the size of the memory you require in bytes. The \texttt{sizeof} function can be used to give you the size of the value you require. For the above example you would perform the following, with \texttt{sizeof(int)} giving you the number of bytes needed to store an \texttt{int} value: \csnipet{p = (int*) malloc(sizeof(int));}
}
\passection{
In Pascal the \texttt{New} procedure is used to allocate memory to a pointer. The \texttt{New} procedure is passed the pointer to allocate, and Pascal uses the information about what it points to, to determine how much space to allocate. For the above example you would perform the following, with \texttt{New} working out that you need to be allocated space for an Integer (the type of value that \texttt{p} points to): \passnipet{New(p);}
}

% subsubsection explicitly_allocating_memory (end)
\clearpage
\subsubsection{Explicitly allocating memory for an array} % (fold)
\label{ssub:explicitly_allocating_memory_for_an_array}

Storing single values on the Heap can be useful, but often you want to be able to allocate enough space for a number of values. Arrays on the Stack must be of a fixed length, so this dynamic allocation allows you to have \textbf{variable length arrays}.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.75\textwidth]{./topics/dynamic-memory/diagrams/AllocateMemory-Array} 
   \caption{You can ask to be allocated a number of values}
   \label{fig:allocate-memory-array}
\end{figure}

\csection{In C you can use \texttt{malloc} to allocate space for a number of element. Alternatively the \texttt{calloc} function from \texttt{stdlib.h} provides direct support for allocating space for an array. This function takes two parameter, the first takes the number of elements to allocation, the second the size of those elements. \texttt{calloc} also \emph{clears} its allocation, setting each byte allocated to 0. The following code allocates 4 integer values as shown in \fref{fig:allocate-memory-array}: \csnipet{p = (int*) malloc(4 * sizeof(int));} or using \texttt{calloc}, which would set all values to 0, you could use \csnipet{p = (int*) calloc(4, sizeof(int));} \newline 
\newline
In C you can use the standard array access mechanisms with pointers to access subsequent elements. So \texttt{p[0]} is the value in the first element of the array dynamically allocated in \fref{fig:allocate-memory-array}, \texttt{p[1]} is the value of the second element, \texttt{p[2]} is the value of the third element, and so on. This relates back to Pointer arithmetic discussed in \sref{ssub:what_is_the_pointer_value_what_can_you_do_with_it_}.
}

\passection{Pascal has built in support for \textbf{dynamic arrays}. You can declare an array without a length, and then call \texttt{SetLength} to specify the size you want allocated. Behind the scenes Pascal will allocate the space for this array on the heap, and will take care of ensuring that it is cleaned up when you no longer have access to it. If you just want a block of memory to work with Pascal also offers a \texttt{GetMem} function.}

% subsubsection explicitly_allocating_memory_for_an_array (end)

\clearpage
\subsubsection{Changing the size of a dynamically allocated array} % (fold)
\label{ssub:changing_the_size_of_a_dynamically_allocated_array}

The advantage of dynamic memory allocation is that you can change your allocations. If you asked for an array of two values, you may later want to be able to expand that array to three or four elements. Alternatively, an array with twenty elements may have some data removed and be shrunk down to only 5 elements. All of this is possible with dynamic memory allocation. You can ask to have the memory you were allocated changed to a different size.

\begin{figure}[h]
   \centering
   \includegraphics[width=0.75\textwidth]{./topics/dynamic-memory/diagrams/AllocateMemory-Array-Resize} 
   \caption{You can change the size of the allocation, growing or shrinking the number of element}
   \label{fig:allocate-memory-array-resize}
\end{figure}

\mynote{
\begin{itemize}
  \item These reallocations will keep the data that was in the array previously. Obviously if the new allocation is smaller than than existing one you will lose some values, but the others are kept.
  \item It is possible that the Operating System will need to move your new allocation, so if you change the size of an array you need to be careful if other pointers refer to elements in their old locations.
\end{itemize}
}

\csection{The \texttt{realloc} function from \texttt{stdlib.h} allows you to change the memory allocation of a pointer. The following C code performs the reallocation shown in \fref{fig:allocate-memory-array-resize}: \csnipet{p = (int*) realloc(p, 2 * sizeof(int));} }

\passection{Pascal \textbf{dynamic arrays} perform this task for you. The \texttt{SetLength} procedure allows you to change the number of elements allocated to a dynamic array as you see fit.}

% subsubsection changing_the_size_of_a_dynamically_allocated_array (end)

% subsection allocating_memory (end)