\documentclass{beamer} %include lhs2TeX.fmt \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{spec} #include #include int main() { int *a, int n; printf("enter the size of the list: "); scanf("%d",&n); a = (int *) malloc(n * sizeof(int)) if( a == NULL) { printf("too bad not enough memory"); return 1; } /* a is now a variable sized array */ sort(a,n); } \end{spec} \end{frame} \begin{frame} \frametitle{General idiom for variable size arrays} \begin{spec} T *a; a = (T *) malloc( n * sizeof(T)) if( a == NULL) { /* not enough memory */ } /* use a */ free(a); \end{spec} \pause \begin{itemize} \item The function malloc allocates the required amount of space, \pause \item |malloc| returns the pointer to the allocated memory if possible or |NULL| otherwise, \pause \item The expression sizeof(T) gives the ``size'' of the type T, \end{itemize} \end{frame} \begin{frame} \frametitle{The |sizeof| operator} \begin{itemize} \item |sizeof(T)| is the memory required to store a value of type |T|. \pause \item |sizeof(a)| where a is of type |T| is same as size of |sizeof(T)|, \pause \item The value of |sizeof| operation is of type |size_t|. \end{itemize} \end{frame} \begin{frame} \begin{code} #include int main() { int a[100]; int *ptr; int b; int c[] = {100,200,3,4,5,6}; printf("sizes of:\n"); printf("\t a is %lud\n",sizeof(a)); printf("\t b is %lud\n",sizeof(b)); printf("\t c is %lud\n",sizeof(c)); printf("\t ptr is %lud\n", sizeof(ptr)); return 0; } \end{code} \end{frame} \begin{frame} \frametitle{The |sizeof| of an array} The size of an array of type |T| and length n is n times the size of |T| \pause \begin{block}{Idiom to find the lenght} \begin{spec} int a[] = {100,200,3,4,5,6} int len = sizeof(a)/sizeof(a[0]); int len = sizeof(a)/sizeof(int); \end{spec} \end{block} \end{frame} \begin{frame} \frametitle{What is the type of the function |malloc|?} \pause \begin{spec} void *malloc(size_t size); \end{spec} \pause \begin{block}{Pointer to void} \begin{itemize} \item Any pointer can be cast to a void pointer. eg. \begin{spec} int *ptr; void *p; p = (void *) ptr; \end{spec} \pause \item If |p| is a |void| pointer which was assigned a pointer to |T| then |p| can be cast back to |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 |void| pointers?} \pause \begin{enumerate} \item Often one wants generic functions like |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}