Top Banner
Instructor: Alexander Stoytchev http://www.cs.iastate.edu/~alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)
42

Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Dec 16, 2015

Download

Documents

Sydney Daniels
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Instructor: Alexander Stoytchev

http://www.cs.iastate.edu/~alex/classes/2008_Fall_185/

CprE 185: Intro to Problem Solving

(using C)

Page 2: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Dynamic Data Structures

CprE 185: Intro to Problem SolvingIowa State University, Ames, IACopyright © Alexander Stoytchev

Page 3: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Administrative Stuff

• HW 10 due this Wednesday

• Ritika will not have office hours this week

• There will be labs this week (also TA evaluations)

• This Wednesday we’ll have a review for the final

Page 4: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Final Exam

• Final Exam: Tuesday Dec 16: 9:45am- 11:45am

• Location: This room.

Page 5: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

HW10: How to clear the screen?

#include <stdio.h>

#include <stdlib.h>

int main()

{

printf("%d\n", 34523);

system("cls"); // system call

system("pause");

}

Page 6: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

HW 10: Student Structure#include <stdio.h>#include <stdlib.h>

typedef struct{ int a;} schedule_t;

typedef struct{ int b; schedule_t schedule[6];} student_t;

int main(){

system("pause");}

Page 7: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Pointers to Structures#include <stdio.h>#include <stdlib.h>

typedef struct node{

int value;} node_t;

int main(){

node_t Node;Node.value = 5; // initialize it to 5printf("value = %d\n", Node.value);

// pointer to the statically allocated struct Nodenode_t *p = &Node;p->value = 6; // change it t0 6printf("value = %d\n", p->value);

}

Page 8: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Arrays of pointers#include <stdio.h>#include <stdlib.h>

int main(){

int* pArray[10];

int a;int b;

pArray[0] = &a;pArray[1] = &b;

system("pause");}

Page 9: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Pointers to functions// function returning an int // and having two arguments (an int and a char)int fun1(int a, char c);

// function returning pointer to an int // and having one int argumentint *fun2(int a);

// pointer to function returning int // and having two arguments (an int and a char)int (*funp)(int a, char c);

// two ways to call the function(*funp)(1,’b’); funp(1,’c’);

Page 10: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Example#include <stdio.h>#include <stdlib.h>

int fun1(int a, int b){ printf("fun1\n");

return a+b;}

int fun2(int a, int b){

printf("fun2\n");return a-b;

}

int main(){ // pointer to function returning int and having two arguments: an int and a float int (*funp)(int a, int b);

funp = fun1; // take the address of the function and assign it to the function pointer(*funp)(1,2); // call the function using the pointer

funp = fun2; // reassign the pointer to point to fun2funp(1,2); // an alternative way of calling a function using a pointer

system("pause");}

Page 11: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Chapter (14)

Page 12: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

What’s going on inside the computer’s memory

[http://computer.howstuffworks.com/c28.htm]

Page 13: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Dynamic Memory

“In computer science, dynamic memory allocation is the allocation of memory storage for use in a computer program during the runtime of that program. It can be seen also as a way of distributing ownership of limited memory resources among many pieces of data and code.”

-From Wikipedia

Page 14: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Dynamic Memory

“Dynamically allocated memory exists until it is released either explicitly by the programmer, exiting a block, or by the garbage collector.

This is in contrast to static memory allocation, which has a fixed duration. It is said that an object so allocated has a dynamic lifetime.”

-From Wikipedia

Page 15: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

[http://msdn.microsoft.com/en-us/library/ms810603.aspx]

Page 16: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Each program usesseveral “types” of memory

• program • stack • shared library • objects • heap

• Some of these are read only, others grow dynamically on as needed basis.

Page 17: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

NOTE: The following slides are QNX specific but the same principles apply to almost any

other modern operating system

• Source:

• http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/memory.html

Page 18: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Process memory layout on an x86.

[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_processmemory.jpg]

Page 19: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Program Memory

[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_program.jpg]

Page 20: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Organization of a program’s memory

[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_stack1.jpg]

Page 21: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Stack Memory

• The Stack memory holds the local variables and parameters your program's functions use.

[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_stack2.jpg]

Page 22: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Shared-library Memory

[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_library.jpg]

Page 23: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Object memory

• “Object memory represents the areas that map into a program's virtual memory space, but this memory may be associated with a physical device. For example, the graphics driver may map the video card's memory to an area of the program's address space: “

[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_object.jpg]

Page 24: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Heap memory

[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_heap2.jpg]

“Heap memory represents the dynamic memory used by programs at runtime. Typically, processes allocate this memory using the malloc(), realloc(), and free() functions.”

Page 25: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

The malloc function

#include <stdlib.h>

void *malloc(size_t size);

The malloc function allocates size number of bytes in the heap memory and returns a pointer to that memory.

Note: The return value is a void pointer so we must cast it to a pointer of the type that we want.

Page 26: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Using malloc

• To allocate an int:int* ip;

ip = (int*) malloc( sizeof(int) );

• To allocate a char:char* cp = (char*) malloc( sizeof(char) );

• To allocate a struct:struct node* np = (struct node*) malloc( sizeof(struct node) );

Page 27: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

How malloc(..) works

[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_heap1.jpg]

Page 28: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

How malloc(..) works

• “When the malloc library receives an allocation request that it can't meet with its existing heap, the library requests additional physical memory from the process manager. As your program frees memory, the library merges adjacent free blocks to form larger free blocks wherever possible. If an entire memory page becomes free as a result, the library returns that page to the system. The heap thus grows and shrinks in 4K increments”

[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_heap2.jpg]

Page 29: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

The free function

• Syntax:

void free ( void * ptr );

• Example:int* ip;

ip = (int*) malloc( sizeof(int) );

free (ip);

Page 30: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Linked Lists

Page 31: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

What is the meaning of this?

typedef struct node

{

int value;

struct node *next;

} node_t;

value next?

Page 32: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Example: Dynamically Allocated Struct#include <stdio.h>#include <stdlib.h>

typedef struct node{

int value; struct node *next;} node_t;

int main(){

// pointer to a dynamically allocated structnode_t *q = (node_t*) malloc (sizeof(node_t));q->value = 10;

q->next = NULL;printf("value = %d\n", q->value);

system("pause");}

Page 33: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

What is the meaning of this code?#include <stdio.h>#include <stdlib.h>

typedef struct node{

int value;struct node *next;

} node_t;

int main(){

node_t *p = (node_t*) malloc (sizeof(node_t));p->value = 37;

node_t *q = (node_t*) malloc (sizeof(node_t));q->value = 99;

node_t *r = (node_t*) malloc (sizeof(node_t));r->value = 12;

p->next = NULL;q->next = p;r->next = q;

}

Page 34: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Singly-Linked List

[http://en.wikipedia.org/wiki/Linked_list]

Page 35: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Inserting a node into a Linked List

Before After

[http://en.wikipedia.org/wiki/Linked_list]

Page 36: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Deleting a node from a linked list

Before:

After:

[http://en.wikipedia.org/wiki/Linked_list]

Page 37: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Doubly-Linked List

[http://en.wikipedia.org/wiki/Linked_list]

Page 38: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

The node struct has 2 pointers

typedef struct

{

int value;

node_t next;

node_t prev;

} node_t;

Page 39: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Inserting a node into a doubly-linked list

Before After

[http://en.wikipedia.org/wiki/Linked_list]

Page 40: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Circularly-linked list

[http://en.wikipedia.org/wiki/Linked_list]

Page 41: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

Algorithm animations

• http://www.cosc.canterbury.ac.nz/mukundan/dsal/appldsal.html

• http://www.ansatt.hig.no/frodeh/algmet/animate.html

Page 42: Instructor: Alexander Stoytchev alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

THE END