CS113 Introduction to C - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CS113 Introduction to C

Description:

void * is a generic pointer type that is returned by all memory functions. By generic one means that void * covers all possible pointers that exist and ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 20
Provided by: ioannisa
Category:

less

Transcript and Presenter's Notes

Title: CS113 Introduction to C


1
CS113Introduction to C
  • Instructor Ioannis A. VetsikasE-mail
    vetsikas_at_cs.cornell.edu
  • Lecture 7 September 8

2
Memory Assignment
  • The following program demonstrates the wrong
    way to assign to a pointer a memory location for
    it to point to.

void main() int a a function_3() printf(
d\n, a ) int function_3() int b b
3 return b
  • The variable a is local to the function
    function_3 and thus when this function ceases to
    exist the memory for a will probably be
    deallocated as well.
  • The correct way is to allocate some memory where
    the pointer can be assigned to as is demonstrated
    by the next program

3
Valid Memory Assignment
include ltstdio.hgt int function_3() void
main() int a a function_3() printf(
d\n, a ) free( a ) int
function_3() int b b (int ) malloc(
sizeof( int )) / NULL? / b 3 return b
4
Memory Allocation Functions
  • All functions are declared in ltstdlib.hgt
  • void malloc(size_t size) allocates size bytes
    and returns a pointer to the new space if
    possible otherwise it returns the null pointer.
  • void calloc(size_t n, size_t size) is same as
    malloc(nsize), but the allocated storage is also
    zeroed (it is slower).
  • void realloc(void ptr, size_t size) changes
    size of previously allocated object to size and
    returns pointer to new space is possible (or NULL
    otherwise)
  • void free(void ptr) deallocates previously
    allocated storage

5
Some suggestions and comments
  • The NULL pointer is a pointer that points to
    nothing. So if pNULL then the statement p is
    going to produce a run-time error.
  • void is a generic pointer type that is returned
    by all memory functions.
  • By generic one means that void covers all
    possible pointers that exist and thus by
    declaring a pointer as generic (void ) you
    suggest that it can accommodate any possible
    pointer type!

6
Some pointer arithmetic
  • int a, b
  • a or b--
  • a 3 or b - 5
  • But the only pointer-pointer operation allowed is
    pointer subtruction (use it for pointers of the
    same type) a-b
  • We can also compare pointers agtb

7
Getting an array of numbers
include ltstdio.hgt void main() int numbers,
size, i, sum 0 printf( "How many numbers? "
) scanf( "d", size ) numbers malloc(
size sizeof( int )) for( i 0 i lt size
i ) scanf( "d", numbersi )
for( i 0 i lt size i ) sum
numbersi printf( "d\n", sum )
free( numbers )
8
Self-referential structures
  • A structure that has as its element(s) pointers
    to the structure itself is called a
    self-referential structure. E.g. a tree
  • typedef struct tree
  • DATA d
  • struct tree left, right
  • tree
  • Other classical structures include stacks,
    queues and linked lists

9
Linear Linked Lists
  • Problem with arrays If we assume that theyre of
    fixed length, need to know maximum length ahead
    of time. Unrealistic.
  • Also, even if we did know the maximum length
    ahead of time, if array was not full, we would be
    wasting memory.
  • One solution Linear linked lists.

define NULL 0 typedef char DATA struct
linked_list DATA d struct linked_list next
/ refers to self / typedef struct
linked_list ELEMENT typedef ELEMENT LINK
10
Linear Linked Lists List creation
/ Uses recursion. From Kelley/Pohl. / LINK
string_to_list( char s ) LINK head if(
s0 \0 ) return NULL else
head malloc( sizeof( ELEMENT )) head-gtd
s0 head-gtnext string_to_list( s 1 )
return head
11
Linear Linked Lists Counting
int count( LINK head ) / recursively
Kelley/Pohl / if( head NULL ) return
0 else return( 1 count( head-gtnext
)) int count_it( LINK head ) int cnt
for( cnt 0 head ! NULL head head-gtnext )
cnt return cnt
12
Linear Linked Lists Lookup
/ from Kelley/Pohl / LINK lookup( DATA c, LINK
head ) if( head NULL ) return NULL
else if( c head-gtd ) return head else
return( lookup( c, head-gtnext ))
13
Linear Linked Lists Insertion/Deletion
/ from Kelley/Pohl / / Assumes q is a
one-element list, and inserts it between p1
and p2, assumed to be consecutive cells in some
list / void insert( LINK p1, LINK p2, LINK q
) p1-gtnext q q-gtnext p2 void
delete_list( LINK head ) if( head ! NULL )
delete_list( head-gtnext ) free( head
)
14
Stacks
  • Another form of data abstraction will implement
    using ideas similar to those used in implementing
    linear linked list.
  • Only two basic operations defined on a stack
    push (insertion), pop (deletion).
  • Access is restricted to the head of the stack.

define NULL 0 typedef char DATA struct
stack DATA d struct stack next /
refers to self / typedef struct stack
ELEMENT typedef ELEMENT LINK
15
Stacks Testing for emptiness, Top element
int isempty( TOP t ) return( t NULL
) DATA vtop( TOP t ) return( t -gt d )
16
Stacks Pop
/ remove top element from the stack / void pop(
TOP t, DATA x ) TOP t1 t if(
!isempty( t1 )) x t1-gtd t
t1-gtnext free( t1 ) else printf(
Empty stack.\n )
17
Stacks Push
/ put an element at the top of the stack / void
push( TOP t, DATA x ) TOP temp temp
malloc( sizeof( ELEMENT )) temp-gtd x
temp-gtnext t t temp
18
Comma Operator
  • Has lowest precedence from any operator in C
  • E.g. a1, b2
  • Will evaluate from left to right and has the
    value and type of the right most assignment
  • Commas separating function arguments variables in
    declarations, etc. are not comma operators and do
    not guarantee left-to-right evaluation order

19
Chapters covered from KR
  • Chapter 1 (all except 1.10)
  • Chapter 2
  • Chapter 3
  • Chapter 4 (only 4.1-4.2)
  • Chapter 5 (5.1-5.9)
  • Chapter 6 (all except 6.9)
Write a Comment
User Comments (0)
About PowerShow.com