\documentclass{beamer}
%
%
\makeatletter
\@ifundefined{lhs2tex.lhs2tex.sty.read}%
  {\@namedef{lhs2tex.lhs2tex.sty.read}{}%
   \newcommand\SkipToFmtEnd{}%
   \newcommand\EndFmtInput{}%
   \long\def\SkipToFmtEnd#1\EndFmtInput{}%
  }\SkipToFmtEnd

\newcommand\ReadOnlyOnce[1]{\@ifundefined{#1}{\@namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
  {<5><6><7><8>cmtex8
   <9>cmtex9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
  {<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
  {<5><6><7><8>cmtt8
   <9>cmbtt9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
  {<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}}	% NEU

\newcommand{\Sp}{\hskip.33334em\relax}
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}

\EndFmtInput
\makeatother
%
\author{Piyush P Kurur\\
  Office no: 224\\
  Dept. of Comp. Sci. and Engg.\\
  IIT Kanpur}


\newcommand{\Token}[2][]{\node(#2#1)[token]{#2};}
\newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};}
\newcommand{\Point}[1]{\node(#1)[point]{#1};}

\usepackage{tikz}
\usetikzlibrary{positioning,shapes,chains,fit}
\usetikzlibrary{shapes.symbols}
\usetikzlibrary{matrix}
\usetikzlibrary{backgrounds}
\usetikzlibrary{shapes.geometric}
\usepackage{multicol}

\usepackage{algorithm2e}
\usepackage{pgfkeys}

\pgfkeys {%
  /tikz/redbox/.style={%
    shape=rectangle,%
    minimum size=1.5em,%
    top color = white, %
    bottom color=red!50!black!50,
    inner sep=0pt,
    draw=black
  },
  /tikz/greybox/.style={ 
    shape=rectangle,%
    minimum width=7em,%
    minimum height=1.5em,
    fill=black!20,
    inner sep=0pt,
    draw=black
  },
  /tikz/blackbox/.style={ 
    shape=rectangle,%
    minimum width=7em,%
    minimum height=1.5em,
    fill=black,
    inner sep=0pt,
    draw=black
  },
  /tikz/whitebox/.style={
    shape=rectangle,
    minimum width=7em,
    minimum height=1.5em,
    draw=black
  },
  /tikz/bluebox/.style={
    shape=rectangle,
    minimum width=7em,
    minimum height=1.5em,
    fill=blue!40,
    draw=black
  },
  /tikz/whitetape/.style={
    shape=rectangle,
    minimum width=4em,
    minimum height=1.5em,
    draw=black
  },
  /tikz/bluearr/.style={
    ->,
    line width=0.25em, 
    draw=blue!40
  },
  /tikz/greenarr/.style={
    ->,
    line width=0.25em, 
    draw=green!40!black!60
  }
}

    
\newcommand{\arraythree}[4]{
  \begin{tikzpicture}[node distance=0]
    \pgfsetstrokeopacity{#4};
    \node[#1](a0#3){#2};
    \node[#1, right=of a0#3](a1#3){#2};
    \node[#1, right=of a1#3](a1#3){#2};
  \end{tikzpicture}
}

\newcommand{\arraytwo}[4]{
  \begin{tikzpicture}[node distance=0]
    \pgfsetstrokeopacity{#4};
    \node[#1](a0#3){#2};
    \node[#1,right=of a0#3]{#2};
  \end{tikzpicture}
}
  
\newcommand{\Array}[0]{
  \arraythree{box}{}{a}{1}
}

\newcommand{\AArray}[0]{%
  \arraythree{bbox}{\Array}{b}{0.5}}
\newcommand{\AAArray}[0]{%
  \arraytwo{bbbox}{\AArray}{c}{0.25}}


\newcommand{\T}[1]{\ensuremath{T\left(#1\right)}}  
\newcommand{\BigOh}[1]{\ensuremath{O\left(#1\right)}}
\title{Fundamentals of Computing: Lecture 22}
\date{September 18, 2009}
\begin{document}
\begin{frame}
  \maketitle
\end{frame}

\begin{frame}
  \frametitle{Dynamic memory allocation}
  
  \begin{enumerate}
  \item Allocating memory as required.
  \item Making programs more flexible, array bounds
    that are runtime dependent.
  \end{enumerate}
\end{frame}

\begin{frame}
  \frametitle{Arbitrary sized array}
  \begin{tabbing}\tt
~~~\char35{}include~\char60{}stdio\char46{}h\char62{}\\
\tt ~~~\char35{}include~\char60{}stdlib\char46{}h\char62{}\\
\tt ~\\
\tt ~~~int~main\char40{}\char41{}\\
\tt ~~~\char123{}\\
\tt ~~~~~int~\char42{}a\char44{}~int~n\char59{}\\
\tt ~~~~~printf\char40{}\char34{}enter~the~size~of~the~list\char58{}~\char34{}\char41{}\char59{}\\
\tt ~~~~~scanf\char40{}\char34{}\char37{}d\char34{}\char44{}\char38{}n\char41{}\char59{}\\
\tt ~~~~~a~\char61{}~\char40{}int~\char42{}\char41{}~malloc\char40{}n~\char42{}~sizeof\char40{}int\char41{}\char41{}\\
\tt ~~~~~if\char40{}~a~\char61{}\char61{}~NULL\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~printf\char40{}\char34{}too~bad~not~enough~memory\char34{}\char41{}\char59{}\\
\tt ~~~~~~~return~1\char59{}\\
\tt ~~~~~\char125{}\\
\tt ~~~~~\char47{}\char42{}~a~is~now~a~variable~sized~array~\char42{}\char47{}\\
\tt ~~~~~sort\char40{}a\char44{}n\char41{}\char59{}\\
\tt ~~~\char125{}
\end{tabbing}
\end{frame}
\begin{frame}
  \frametitle{General idiom for variable size arrays}

  \begin{tabbing}\tt
~~~~~T~\char42{}a\char59{}\\
\tt ~~~~~a~\char61{}~\char40{}T~\char42{}\char41{}~malloc\char40{}~n~\char42{}~sizeof\char40{}T\char41{}\char41{}\\
\tt ~~~~~if\char40{}~a~\char61{}\char61{}~NULL\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~\char47{}\char42{}~not~enough~memory~\char42{}\char47{}\\
\tt ~~~~~\char125{}\\
\tt ~~~~~\char47{}\char42{}~use~a~\char42{}\char47{}\\
\tt ~~~~~free\char40{}a\char41{}\char59{}
\end{tabbing}
  \pause
  \begin{itemize}
  \item The function malloc allocates the required amount of space,
    \pause
  \item \text{\tt malloc} returns the pointer to the allocated memory if possible
    or \text{\tt NULL} otherwise,
    \pause
  \item The expression sizeof(T) gives the ``size'' of the type T,
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{The \text{\tt sizeof} operator}
  
  \begin{itemize}
  \item \text{\tt sizeof\char40{}T\char41{}} is the memory required to store a value of type
    \text{\tt T}.
    \pause
  \item \text{\tt sizeof\char40{}a\char41{}} where a is of type \text{\tt T} is same as size of
    \text{\tt sizeof\char40{}T\char41{}},
    \pause
  \item The value of \text{\tt sizeof} operation is of type \text{\tt size\char95{}t}.
  \end{itemize}

\end{frame}

\begin{frame}
  \begin{tabbing}\tt
~~~~~\char35{}include\char60{}stdio\char46{}h\char62{}\\
\tt ~\\
\tt ~~~~~int~main\char40{}\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~int~a\char91{}100\char93{}\char59{}\\
\tt ~~~~~~~int~\char42{}ptr\char59{}\\
\tt ~~~~~~~int~b\char59{}\\
\tt ~~~~~~~int~c\char91{}\char93{}~\char61{}~\char123{}100\char44{}200\char44{}3\char44{}4\char44{}5\char44{}6\char125{}\char59{}\\
\tt ~~~~~~~printf\char40{}\char34{}sizes~of\char58{}\char92{}n\char34{}\char41{}\char59{}\\
\tt ~~~~~~~printf\char40{}\char34{}\char92{}t~a~is~~~\char37{}lud\char92{}n\char34{}\char44{}sizeof\char40{}a\char41{}\char41{}\char59{}\\
\tt ~~~~~~~printf\char40{}\char34{}\char92{}t~b~is~~~\char37{}lud\char92{}n\char34{}\char44{}sizeof\char40{}b\char41{}\char41{}\char59{}\\
\tt ~~~~~~~printf\char40{}\char34{}\char92{}t~c~is~~~\char37{}lud\char92{}n\char34{}\char44{}sizeof\char40{}c\char41{}\char41{}\char59{}\\
\tt ~~~~~~~printf\char40{}\char34{}\char92{}t~ptr~is~\char37{}lud\char92{}n\char34{}\char44{}~sizeof\char40{}ptr\char41{}\char41{}\char59{}\\
\tt ~~~~~~~return~0\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}
\end{frame}

\begin{frame}
  \frametitle{The \text{\tt sizeof} of an array}
  The size of an array of type \text{\tt T} and length n is 
  n times the size of \text{\tt T}

  \pause
  \begin{block}{Idiom to find the lenght}
  \begin{tabbing}\tt
~~~~~int~a\char91{}\char93{}~\char61{}~\char123{}100\char44{}200\char44{}3\char44{}4\char44{}5\char44{}6\char125{}\\
\tt ~~~~~int~len~\char61{}~sizeof\char40{}a\char41{}\char47{}sizeof\char40{}a\char91{}0\char93{}\char41{}\char59{}\\
\tt ~~~~~int~len~\char61{}~sizeof\char40{}a\char41{}\char47{}sizeof\char40{}int\char41{}\char59{}
\end{tabbing}
  \end{block}
\end{frame}
\begin{frame}
  \frametitle{What is the type of the function \text{\tt malloc}?}
  
  \pause
  \begin{tabbing}\tt
~~~~~void~\char42{}malloc\char40{}size\char95{}t~size\char41{}\char59{}
\end{tabbing}
  
  \pause
  
  \begin{block}{Pointer to void}
    \begin{itemize}
    \item Any pointer can be cast to a void pointer.
      eg.
      \begin{tabbing}\tt
~~~~~~~~~int~\char42{}ptr\char59{}\\
\tt ~~~~~~~~~void~\char42{}p\char59{}\\
\tt ~~~~~~~~~p~\char61{}~\char40{}void~\char42{}\char41{}~ptr\char59{}
\end{tabbing}
      \pause
    \item If \text{\tt p} is a \text{\tt void} pointer which was assigned a
      pointer to \text{\tt T} then \text{\tt p} can be cast back to \text{\tt T}.
      \pause
    \item A void pointer cannot be dereferenced
      \pause
    \item No pointer arithmetic is allowed on void pointer.
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}
  \frametitle{Why \text{\tt void} pointers?}
  \pause
  \begin{enumerate}
  \item Often one wants generic functions like \text{\tt malloc} that really
    does not care of the pointer type that it is manipulating.
    \pause
  \item The hardware may not support arbitrary conversion due to
    alignment restriction.
    \pause
  \end{enumerate}
\end{frame}
\end{document}
