homework finishing k&r chapter 5 today –skipping sections 5.7-5.9 for now –not covering...

17
Homework • Finishing K&R Chapter 5 today – Skipping sections 5.7-5.9 for now – Not covering section 5.12 • Starting K&R Chapter 6 next

Upload: marcus-anderson

Post on 13-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

Homework

• Finishing K&R Chapter 5 today– Skipping sections 5.7-5.9 for now– Not covering section 5.12

• Starting K&R Chapter 6 next

Page 2: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

malloc( ) and free( )• To get a pointer p to a block of memory

that is n characters in length, program calls p = malloc(n);

• When it is finished with that memory, the program returns it by calling free(p);

• Sounds simple, huh?

• It is NOT so simple!

Page 3: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

malloc( ) and free( )

• malloc returns a pointer (void *) that points to a memory block of n bytes

• If you need a pointer to n of a specific type, you must request a memory block in size of the type and cast pointer returned by mallocint *p;

p = (int *) malloc(n * sizeof(int));

Page 4: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

malloc and free

• If it can not provide the requested memory, malloc returns a NULL pointer value

• If you dereference a NULL pointer to access memory System Crash!!

• Always check to be sure that the pointer returned by malloc is NOT equal to NULL

• If pointer is NULL, code must take appropriate recovery action to handle lack of memory

Page 5: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

malloc and free

• Call to free does not clear the program’s pointer to the memory block, so it is now a “stale” pointer

• If program uses pointer after free( ) by accessing or setting memory via pointer, it could overwrite data owned by another program System Crash!

• If program calls free again with the same pointer, it releases memory possibly owned by a different program now System Crash!

• SHOULD set pointer to NULL after calling free( )

Page 6: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

malloc and free

• However, if you set the pointer to a memory block to NULL before calling free, you have caused the system to lose the memory forever

• This is called a memory leak!!

• If it happens enough times System Crash!!

• MUST not clear or overwrite a pointer to a memory block before calling free!!

Page 7: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

malloc( ) and free( )

• Memory model for malloc( ) and free( )Before call to malloc( ) and after call to free( ):

allocbuf:

After call to malloc( ) and before call to free( ):

allocbuf:

In use

In use

Free

Free

In use Free

In use In use Free

Page 8: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

malloc( ) and free( )

• Fragmentation with malloc( ) and free( )Before call to malloc( ) for a large memory block:

allocbuf:

malloc can NOT provide a large contiguous blockof memory - even though there is theoreticallysufficient free memory! Memory is fragmented!!

In use Free In use Free In use Free In use

Page 9: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

malloc and free

• The malloc memory fragmentation problem is not easy to solve!!

• Not possible to “defragment” malloc memory as you would do for a disk

• On a disk, the pointers to memory are in the disk file allocation table and can be changed

• With malloc, programs are holding pointers to memory they own - so can’t be changed!!

Page 10: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

Pointers to Functions, K&R 5.11

• Function prototype with pointer to functionvoid qsort ( …, int (*comp) (void *, void *));

• Function call passing a pointer to functionqsort( … , (int (*) (void *, void *)) strcmp);

This is a cast to function pointer of strcmp

• Within qsort(), function is called via a pointerif ((*comp) (v[i], v[left]) < 0) …

Page 11: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

Pointers to Functions

• Initialize a pointer to a function

/* function pointer *fooptr = cast of foo to func ptr */

int (*fooptr) (int *, int*) = (int (*) (int *, int *)) foo;

• Call the function foo via the pointer to it

(*fooptr) (to, from);

Page 12: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

structs, K&R 6• A struct is a collection of variables, possibly of

different types, grouped under a single name for common reference as a unit.struct point { /* with optional tag */

int x; /* member x */

int y; /* member y */} pt, q; /* variable names */

or struct { /* w/o optional tag */

int x, y; /* two members */} pt, q; /* variable names */

Page 13: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

structs

• The defined struct point is like a new “type”

• With the tag, point, can declare other variables:

struct point pt1, maxpt = {320, 200};

• Reference to struct members:

pt1.x = 320; pt1.y = 200; /* alternate init */

printf("(%d, %d)\n", pt1.x, pt1.y);

/* prints out as (320, 200) */

Page 14: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

structs

• Defining a struct inside a struct (nesting).

struct rect {

struct point pt1; /* lower left */

struct point pt2; /* upper right */

};

struct rect box;/* declare box as a rect */

Page 15: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

structs• pt1 is lower left hand corner of box

• pt2 is upper right hand corner of box:box.pt1.x < box.pt2.x box.pt1.y < box.pt2.y

y

xbox.pt1.x box.pt2.x

box.pt1.y

box.pt2.y

Page 16: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

structs

/* Find area of a rectangle */

int area = rectarea (box);

int rectarea (struct rect x)

{

return (x.pt2.x - x.pt1.x) * (x.pt2.y - x.pt1.y);

}

Page 17: Homework Finishing K&R Chapter 5 today –Skipping sections 5.7-5.9 for now –Not covering section 5.12 Starting K&R Chapter 6 next

structs

• Memory allocation for structs– Two point structs pt1 and pt2

– One rect struct box containing two point structs

pt1

pt1.x pt1.y pt2.x pt2.y

pt2

pt1

pt1.x pt1.y pt2.x pt2.y

pt2box