Advanced Pointer Topics - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Advanced Pointer Topics

Description:

Advanced Pointer Topics – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 21
Provided by: MikeKat5
Category:

less

Transcript and Presenter's Notes

Title: Advanced Pointer Topics


1
Advanced Pointer Topics
2
Pointers to Pointers
  • A pointer variable is a variable that takes some
    memory address as its value. Therefore, you can
    have another pointer pointing to it.
  • int x
  • int px
  • int ppx
  • ppx px
  • px x / i.e. ppx x /
  • ppx 10 / i.e. px 10 i.e. x10 /
  • ppx (int ) malloc(sizeof(int ))
  • ppx 20 / Wrong, since ppx is
    uninitialized! /

3
Arrays of Pointers (1)
  • If we have an array of structures, each structure
    can potentially be very big.
  • To sort such an array, a lot of memory copying
    and movements are necessary, which can be
    expensive.
  • For efficiency, we can use array of pointers
    instead
  • struct book
  • float price
  • char abstract5000
  • struct book book_ary1000
  • struct book pbook_ary1000
  • for(i0ilt1000i)
  • pbook_aryi book_aryi

4
Arrays of Pointers (2)
  • void my_sort(struct book pbook_ary , int
    size)
  • int i, j
  • struct book p
  • for(i1iltsizei)
  • ppbook_aryi
  • for(ji-1jgt0j--)
  • if(pbook_ary j -gt price gt p -gt price)
  • pbook_ary j1 pbook_ary j
  • else
  • break
  • pbook_ary j1 p

5
Arrays of Pointers (3)
  • struct book search_range(struct book
    pbook_ary , int size, float low, float high,
    int num)
  • int i, j
  • for(i0iltsizei)
  • if(pbook_aryi -gt price gt low) break
  • for( jsize jgt0j--)
  • if(pbook_ary j -gt price lt high) break
  • / i , i1, , j are the elements in the range
    /
  • num j i 1
  • return pbook_ary i

6
Dynamic Two Dimensional Arrays
  • int ary
  • int m, n
  • srand( time(NULL) )
  • m rand( ) 5000 10
  • n rand( ) 5000 10
  • ary (int ) malloc( m sizeof(int ) )
  • for( j 0 jlt m j)
  • ary j (int ) malloc (n sizeof(int))
  • ary34 6
  • ( ( ary 3) 4) 6
  • ary-gt3-gt4 6 / NO! You can not do this /

7
const Pointers (1)
  • The const keyword has a different meaning when
    applied to pointers.
  • void test( const int k, const int m)
  • k / 1 /
  • (m) / 2 /
  • m / 3 /
  • printf("d,d", k, m)
  • The compiler will warn you about the 1st and 2nd
    increments, but not the 3rd .

8
const Pointers (2)
  • The reason we use const before parameters is to
    indicate that we will not modify the value of the
    corresponding parameter inside the function.
  • For example we would not worry about the
    format_str is going to be modified by printf when
    we look at its prototype
  • int printf(const char format_str, )

9
Pointers to Functions (1)
  • Since a pointer merely contains an address, it
    can point to anything.
  • A function also has an address -- it must be
    loaded in to memory somewhere to be executed.
  • So, we can also point a pointer to a function.
  • int (compare)(int, int)

1
1. Compare is a pointer 2. To a function 3. That
returns an int value
2
3
10
Pointers to Functions (2)
  • typedef struct
  • float price
  • char title100
  • book
  • int (ptr_comp)(const book , const book )
  • / compare with
  • int ptr_comp(const book , const book )
  • /
  • Do not forget to initialize the pointer -- point
    the pointer to a real function!

11
Pointers to Functions (3)
  • include ltstring.hgt
  • int compare_price(const book p, const book q)
  • return p-gtprice-q-gtprice
  • int compare_title(const book p, const book q)
  • return strcmp(p-gttitle,q-gt title)
  • int main( )
  • book a, b
  • a.price19.99
  • strcpy(a.title, "unix")
  • b.price20.00
  • strcpy(b.title, "c")
  • ptr_comp compare_price
  • printf("d", ptr_comp(a, b))
  • ptr_comp compare_title
  • printf("d", ptr_comp(a, b))
  • return 0

12
Example The qsort() Function (1)
  • Often, you want to sort something using the quick
    sort algorithm. C provides a qsort() function in
    its standard library. Here is the prototype
  • SYNOPSIS
  • include ltstdlib.hgt
  • void qsort(void base, size_t nel, size_t
    width,
  • int (compar)(const void , const void ) )
  • The base argument points to the element at the
    base of the array to sort.
  • The nel argument is the number of elements in the
    table. The width argument specifies the size of
    each element in bytes.
  • The compar argument is a pointer to the
    comparison function, which is called with two
    arguments that point to the elements being
    compared.

13
Example The qsort() Function (2)
  • An example
  • include ltstdlib.hgt
  • book my_books1000
  • qsort(my_books, 1000, sizeof(book),
    compare_price)
  • qsort(my_books, 1000, sizeof(book),
    compare_title)

14
Deallocating Dynamic Structures
  • For every call to malloc used to build a
    dynamically allocated structure, there should be
    a corresponding call to free.
  • A table inside malloc and free keeps track of the
    starting addresses of all allocated blocks from
    the heap, along with their sizes.
  • When an address is passed to free, it is looked
    up in the table, and the correct amount of space
    is deallocated.
  • You cannot deallocate just part of a string or
    any other allocated block!

15
Example Code for Freeing a List of Items
  • In ItemTypes.h
  • typedef struct listItem
  • int count
  • char str
  • Item
  • In ItemInterface.h
  • include "ItemTypes.h"
  • Item createItem( char s )
  • void freeItem( Item it )
  • void incCounter( Item it )
  • char getItemStr( Item it )
  • ...
  • In ItemImplementation.c
  • include ltstring.hgt
  • include "ItemInterface.h"
  • ...
  • Item createItem(char s)
  • Item it (Item )malloc(
  • sizeof(item) )
  • it-gtcount 1
  • it-gtstr strdup( s )
  • return it

16
Example Code for Freeing a List of Items
  • In ItemImplementation.c
  • void freeItem( Item it )
  • free( it-gtstr )
  • free( it )
  • void incCounter( Item it )
  • (it-gtcount)
  • char getItemStr( Item it )
  • return it-gtstr
  • ...
  • In NodeTypes.h
  • include "ItemTypes.h"
  • typedef struct node
  • Item value
  • struct node next
  • Node
  • In NodeInterface.h
  • void freeNode( Node n )
  • Item getValue( Node n )
  • Node getPtr( Node n )
  • ...

17
Example Code for Freeing a List of Items
  • In NodeImplementation.c
  • void freeNode( Node n )
  • freeItem( n-gtvalue )
  • free( n )
  • Item getValue( Node n )
  • return n-gtvalue
  • Node getPtr( Node n )
  • return n-gtnext
  • ...
  • In ListTypes.h
  • include "NodeTypes.h"
  • typedef struct list
  • int size
  • Node head
  • Node tail
  • List
  • In ListInterface.h
  • void freeList( List n )
  • ...

18
Example Code for Freeing a List of Items
  • In ListImplementation.c
  • void freeChain( Node n )
  • / a private function --
  • not in List interface /
  • if ( n ! NULL )
  • freeChain(getPtr( n ))
  • freeNode( n )
  • In ListImplementation.c
  • void freeList( List ll )
  • freeChain( ll-gthead )
  • free( ll )

19
Example Code for Freeing a List of Items
  • In the previous example, it was assumed that
    everything in the list was allocated dynamically,
    including the list header.
  • If a list had been declared using LinkedList x
    the function freeList could not be used to
    deallocate the dynamically allocated space
    contained inside the list.
  • The list interface could provide a resetList
    function to clean up such a structure.

20
Example Code for Freeing a List of Items
  • void resetList( List ll )
  • / Free up the list that ll currently points at,
    and reset
  • ll to be the empty List.
  • /
  • freeChain( ll-gthead )
  • ll-gthead ll-gttail NULL
  • ll-gtsize 0
Write a Comment
User Comments (0)
About PowerShow.com