Storage Allocation and Linked Lists - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Storage Allocation and Linked Lists

Description:

Storage Allocation and Linked Lists – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 44
Provided by: lcs4
Category:

less

Transcript and Presenter's Notes

Title: Storage Allocation and Linked Lists


1
Chapter 11
  • Storage Allocation and Linked Lists

2
Compile-Time Run-TimeStorage Allocation
  • int n,
  • float x
  • compile-time storage allocation
  • storage is allocated at compile time before run
    time
  • Run-time storage allocation
  • storage is allocated at run time
  • Use
  • malloc
  • calloc

3
In C Storage is allocated when variables are
defined ? compiler-time storage
allocation int abc struct client clients100
program to run for all applications ? maximum
size of definition has to be used
define maxsize 200 float amaxsizemaxsize
4
malloc
  • int ptr
  • ptr malloc (sizeof(int))
  • malloc( sizeof (datatype))
  • returns a pointer to the allocated storage
  • returns NULL if fails
  • Run-time allocation
  • char alpha
  • int num
  • char r
  • rgetchar()
  • if(rc)
  • alphamalloc(sizeof(char))
  • else
  • nummalloc(sizeof(int))

5
Allocate structure at run time
  • typedef struct complex
  • float real
  • float imag
  • Complex
  • Complex ptrc
  • ptrc malloc(sizeof(Complex))
  • ptrc -gt real3.14
  • ptrc -gt imag 2.11
  • free (ptrc)

6
Run-time storage allocation (dynamic memory
allocation)
dynamically allocate and deallocate memory when
required
  • ptr malloc ( number_of_bytes_to_be_allocated)
  • ptr if successful ? a pointer to the allocated
    storage
  • if unsufficient memory is available ? null

ptr
  • traditionally, malloc() returns the address of
    type pointer-to-char
  • ptr malloc(nsizeof(float))

7
  • number_of_bytes_to_be_allocated
  • eg. 100 sizeof (datatype)
  • sizeof (variable)

ptr malloc ( 100) ptr malloc (sizeof
(int)) mmalloc( sizeof(complex1))
struct complex float a float b complex1
8
calloc
  • int ptr1,ptr2
  • ptr1 malloc(sizeof(int))
  • ptr2 calloc(20,sizeof(int))
  • Allocates 20 int storage cells
  • Returns the address of first cell
  • Returns NULL if fails

9
Two ways to access storage
  • compile-time storage (name, pointer)
  • int a, a_ptr
  • a_ptra
  • printf(d d , a, a_ptr)
  • running-time storage (pointer only)
  • a_ptr malloc(sizeof(int))

10
Garbage
  • int ptr
  • ptr malloc (sizeof(int))
  • ptr 999
  • ptr malloc (sizeof(int))
  • ptr -777
  • printf(d, ptr)
  • Prints -777
  • The cell storing 999 cannot be accessed (garbage)
  • Remedy
  • int ptr1,ptr2
  • ptr1 malloc (sizeof(int))
  • ptr1999
  • ptr2 malloc (sizeof(int))
  • ptr2 -777
  • printf(dd, ptr1,ptr2)

11
Release run-time storage free(ptr)
deallocate the space pointed to by ptr
ptr  points to a block of memory previously
allocated by calloc() or malloc() int
ptr ptr malloc (sizeof(int))
free(ptr)
12
include ltstdio.hgt include ltstdlib.hgt typedef
int Matrix / hide the int data type /
void store( char file, Matrix m1, Matrix m2,
int n ), mult( Matrix m1, Matrix m2, Matrix m3,
int n ), print( Matrix m, int n ), freeMatrix(
Matrix m, int n ) Matrix createMatrix( int n )
main() int n char filename FILENAME_MAX
Matrix m1, m2, m3 / prompt user for side and
file name / printf( "Matrix size and file name
" ) scanf( "d s", n, filename ) / create
the matrices / m1 createMatrix( n ) m2
createMatrix( n ) m3 createMatrix( n ) if (
NULL m1 NULL m2 NULL m3 ) /
success? / fprintf( stderr, "Insufficient
storage. Exiting...\n\n" ) exit( EXIT_SUCCESS
) store( filename, m1, m2, n ) / read data
into m1 and m2 / mult( m1, m2, m3, n ) / m3
m1 m2 / print( m3, n ) / print m3 /
freeMatrix( m1, n ) freeMatrix( m2, n )
freeMatrix( m3, n ) / free storage for m1, m2,
and m3 / return EXIT_SUCCESS
13
/ Create a matrix of side n, i.e., a matrix
that contains n x n int cells. / Matrix
createMatrix( int n ) Matrix m int i m (
Matrix ) malloc( n sizeof ( int ) ) if (
NULL m ) return NULL for ( i 0 i lt n
i ) m i ( int ) malloc( n sizeof (
int ) ) if ( NULL m i ) freeMatrix( m,
i ) / avoid garbage / return NULL
return m
14
/ Free all storage associated with matrix m. /
void freeMatrix( Matrix m, int n ) int i
for ( i 0 i lt n i ) free( m i ) free(
m )
15
/ Multiply matrix m1 by matrix m2, storing the
product in matrix m3, where all three matrices
are n by n in size void mult( Matrix m1,
Matrix m2, Matrix m3, int n ) int i, j, k
for ( i 0 i lt n i ) for ( j 0 j lt n
j ) m3 i j 0 for ( k 0 k lt n
k ) m3 i j m1 i k m2 k j

16
Linked Lists
  • Single linked list
  • Linked list
  • N1, N2,, Nn nodes
  • plus address of N1
  • Nk node contains the address of N(k1) node
  • Nn node (last) contains null

17
Example
  • typedef struct cat
  • char name10
  • struct cat next
  • CAT
  • CAT cat1,cat2,cat3
  • CAT start,ptr
  • strcpy(cat1.name,abc)
  • strcpy(cat2.name,nbc)
  • strcpy(cat3.name,cbs)
  • cat1.nextcat2
  • cat2.nextcat3
  • cat3.nextNULL
  • startcat1
  • ptrstart
  • printf(s, ptr-gt name)
  • ptrptr -gt next
  • printf(s, ptr -gt name)
  • Prints
  • abcnbc

18
Example-modified
  • typedef struct cat
  • char name10
  • struct cat next
  • CAT
  • CAT get_cat(void)
  • main()
  • CAT start
  • startget_cat()
  • CAT get_cat(void)
  • CAT curr,first
  • int r
  • curr first malloc(sizeof(CAT))
  • printf(\n enter name)
  • scanf(s,curr -gt name)
  • printf(\nadd another?(1y,0no))
  • scanf(d,r)

19
Example-modified
  • while ( r )
  • curr -gt next malloc(sizeof(CAT))
  • curr curr -gt next
  • printf(\n enter name)
  • scanf(s,curr -gt name)
  • printf(\nadd another?(1y,0no))
  • scanf(d,r)
  • curr -gtnextNULL
  • return first
  • This modified code assigns storage at run time
    and does not require setting maximum number of
    storage cells. That is, the number of cells is
    flexible.

20
Operations on Linked Listfind ptr of n-th node
  • typedef struct node
  • int num
  • struct node next
  • NODE
  • / find the n-th node /
  • NODE find_n(NODE ptr, int n)
  • / ptr is pointer to first node /
  • if (nlt1)
  • return NULL
  • while (- - n ptr ! NULL)
  • ptr ptr -gt next
  • return ptr
  • Let n2 and assume that we have 3 nodes
  • --n 1 ptr not NULL
  • ptr ptr -gtnext
  • --n 0 return
  • ptr points to the second node

21
Add a node to existing linked list
  • NODE find_n(NODE ptr, int n)
  • NODE add_n(NODE ptr, NODE new, int n)
  • NODE pred
  • if (n1)
  • new -gtnext ptr
  • return new
  • ptr pointer to first node
  • new pointer to new node to be added
  • nposition of the new node in the revised list
  • returns pointer to the first node
  • returns NULL if fails

22
  • predfind_n(ptr, n-1)
  • if(pred NULL)
  • return NULL
  • new -gtnext pred -gt next
  • pred -gt next new
  • return ptr
  • Before
  • after

new
n-1
n
23
Delete a node to existing linked list
  • NODE find_n(NODE ptr, int n)
  • NODE del_n(NODE ptr, int n)
  • NODE pred, old
  • if (n1)
  • old ptr
  • ptr ptr -gt next
  • else
  • ptr pointer to first node
  • nposition of the node to be deleted
  • returns pointer to the first node

24
  • predfind_n(ptr, n-1)
  • if(pred NULLpred-gtnext NULL)
  • return ptr
  • old pred -gt next
  • pred -gt next old-gtnext
  • free(old)
  • return ptr

old
n-1
n
25
ELEE 3331 "Sample" Exam 2
I.(10pts)Select the best answer to each of the
following concerning ANSI C and circle your
choice.
A. Which type of variables are initialized to
zero by the C compiler? 1. auto 2. register 3. loc
al 4. internal 5. static 6. all variables are
initialized to zero
Ans 5
26
B. Given the declaration float x,
zx which scanf would successfully read a
float value into x? 1. scanf(f,x) 2. scanf(f
,z) 3. scanf(f,z) 4. scanf(f,z) 5. all
of the above
Ans 2
27
C. The sizeof operator used with an array name
will give 1. the number of elements declared to
be in the array 2. the number of elements
actually in the array 3. the number of bytes
allocated to the array 4. the number of bits
allocated to the array 5. the number of
dimensions of the array
Ans 3
28
D. Given the following declaration char
t15a,t2t1,t3Lois,t4Rhoda Baggs
which of the following statements would be
valid? 1. t1 t3 2. t2 t3 3. t2 t4
(there was a typo here, there was supposed to
have been the in front) 4. t3 t4
Ans 2
29
E. If temp is a member of the structure weather
and the statement addweat weather has
been executed, then which of the following
correctly references temp? 1. weather-gttemp 2. (
weather).temp 3. addweat.temp 4. addweat-gttemp
Ans 4
30
II.(6pts)Short answer 1. Assuming the first value
in the array s is contained in s0, give two
different notations for referring to the 3rd
element of s.
s2 (s2)
31
2. Given the declaration int x 25, y
The statement y x results in an access
violation error. Explain the problem and
indicate how it could be corrected, assuming we
want y to point to a different memory location
than x.
no memory is allocated for what y points to so
either precede with y(int)malloc(sizeof(int))
or declare separate variable and have z point to
it first int z yz
32
III.Indicate the output that would be produced by
each of the following. Consider each case
separately. A.(6pts) include ltstdio.hgt include
ltstring.hgt include ltmath.hgt int k 55 main(
) int i 1, j4, m int f(int ) m
f(i) printf("\n d d d \n", j,m,k) int
f(int j) static int k 2 if(jlt3)
kf(j1) return(k)
f(1) k2 kkf(2) return k
f(2) kkf(3) return k
f(3) k2 return k
output 4 8 55
33
B.(8pts) include ltstdio.hgt main( ) float
a23 3.1,7.4,2.8,1.9,6.3,9.8
printf(\nf f f f \n,((a1)1),((a)1),(
(a1)),((a)1)1)
3.1 7.4 2.8 1.9 6.3 9.8
-- point to raw -- point to column
a
(a1)
a
output 6.3
7.4
1.9
8.4
(a1)
34
merry.dat 1 pole 11/2 yard one
gram 0.035 ounces 1 gallon equals 3.79
liters 1 electron 1/1837 photons
C.(4pts) include ltstdio.hgt include
ltstring.hgt include ltmath.hgt main(
) int n float q1,q2 char u116,
u216 FILE fptr fptrfopen(merry.dat,
r) while((nfscanf(fptr,"f 15s f
15s", q1,u1,q2,u2))!EOF)
printf("d f f s s \n",n,q1,q2,u1,u2)
fscanf(fptr,"\n") fclose(fptr)
Output
4 1.0 11.0 pole /2
0 1.0 11.0 pole /2
2 1.0 11.0 gallon /2
4 1.0 1.0 electron /1837
35
D.(10pts)Show exact spacing for this output by
filling in the grid below main( ) char
ch 'X' char st "Hello World" int
k 52 float f 12.978 double d
-97.4583 printf("3d and 7.2fc\n",k,f,ch)
printf("-14.5s\n\n",st)
printf("8.2f",d) printf(".2f.2e",f,f)
output cols 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8
9 0 1 2 3 4 5 6 7 8 9 0
line 1 _ 5 2 _ a n d _ _ _ 1 2 . 9 8 X _ _
_ _ _ _ _ _ _ _ _ _ _ _
line 2 H e l l o _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
line 3 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _
line 4 _ _ - 9 7 . 4 6 1 2 . 9 8 1 . 3 0 e
0 1 _ _ _ _ _ _ _ _ _
36
IV.(15pts)Long Integers Suppose the following
declaration is available globally struct
longint int digit int power
struct longint next This
template has been used in creating a linked list
representation of large integers, where the
pointer to the list "points" to the least
significant digit. For example, the number
3,421,000,000 would be represented as Write a
C function whose prototype is struct
longint bigger(struct longint int1, struct
longint int2) which will accept pointers to the
beginning of two large integer linked lists and
return a pointer to the beginning of the list
which represents the larger integer.
start
37
struct longint bigger(struct longint in1,struct
longint in2) int n10,n20,i struct longint
p1,p2 p1 in1 while(p1-gtnext!NULL)
n1p1p1-gtnext p2in2
while(p2-gtnext!NULL) n2p2p2-gtnext
if(p1-gtpowergtp2-gtpower)return in1
if(p2-gtpowergtp1-gtpower)return in2
if(p1-gtdigitgtp2-gtdigit)return in1
if(p2-gtdigitgtp1-gtdigit)return in2
38
while(n1gt0n2gt0) p1in1
for(i0iltn1i) p1p1-gtnext p2in2
for(i0iltn2i)p2p2-gtnext
if(p1-gtpowergtp2-gtpower)return in1
if(p2-gtpowergtp1-gtpower)return in2
if(p1-gtdigitgtp2-gtdigit)return in1
if(p2-gtdigitgtp1-gtdigit)return in2
n1--n2-- if(n1lt0n2gt0)return(in2)
if(n2lt0n1gt0)return(in1) if(n1lt0n2lt0)
printf("\n values same\n") return(in1)
39
V.(15pts)Cold Spots Suppose that the following
declarations are available globally struct
citytemps char city35
float temp366 struct coldcnts char
city35 int count Suppose
further that a data file named cold.dat has been
prepared containing the names of each city in
Texas, along with that city's minimum
temperatures(in Fahrenheit) for each day of the
past year. The name of each city starts on a new
line and is terminated by a semicolon ().
Following the name are the minimum temperatures
for that city. For example Austin 44 48 38
... Beeville 38 37 35 ... San Antonio 51 49 40
..
40
Write a complete C program that will input the
data into an array of type struct citytemps,
where each array element corresponds to a city.
Then create a new array of type struct coldcnts,
consisting of the name of the city and the number
of days that city's minimum temperature was at or
below freezing. Do not include in this new array
any cities which had all days above freezing.
Then print this new array, one city per line.
include ltstdio.hgt include ltstring.hgt struct
citytemps char city35 float
temp366 struct coldcnts char
city35 /not required/ int count
41
main( ) FILE fin,fout struct citytemps
cities500 struct coldcnts freeze500 int
i,j,k,nfrz0,ncity0,done0,nv float t char
name35 finfopen("city.dat","r")
foutfopen("city.out","w") while(done0) nvfs
canf(fin,"",name) if(name1'\0')nv0
if(nvgt0) strcpy(citiesncity.city,name)
fprintf(fout,"\n city d s",ncity1,citiesncity
.city) for(i0ilt366i)
fscanf(fin,"f",t) citiesncity.tempit
ncity
42
else done1 for(i0iltncityi)
k0 for(j0jlt366j) if(citiesi.tempj
lt32) k if(kgt0) freezenfrz.countk
strcpy(freezenfrz.city,citiesi.city)
nfrz for(i0iltnfrzi)
fprintf(fout,"\n s d",freezei.city,freezei.c
ount)
43
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com