Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Similar presentations


Presentation on theme: "Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)"— Presentation transcript:

1 Instructor: Alexander Stoytchev http://www.ece.iastate.edu/~alexs/classes/2009_Fall_185/ CprE 185: Intro to Problem Solving (using C)

2 Dynamic Data Structures CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

3 Administrative Stuff There will be labs this week (also TA evaluations) This Wednesday we’ll have a review for the final

4 Final Exam Final Exam: Thursday Dec 17: 2:15- 4:15 pm Location: This room. The final is worth 25% of your grade. The best way to fail this class is to not show up.

5 Final Format True/False(10 x 1p each = 10p) Fun Stuff( 3 x 5p each = 15p) Short answers( 5 x 3p each = 15p) Code Snippets( 4 x 5p each = 20p) What is the output( 3 x 5p each = 15p) Program 1(15p) Program 2(20p) Program 3(20p) TOTAL (130p)

6 Final Format You don’t need to get all 130 points to get an A 100 is a 100 You must get at least 65 points in order to pass this exam

7 How do I clear the screen? #include int main() { printf("%d\n", 34523); system("cls"); // system call system("pause"); }

8 HW 10: Student Structure #include typedef struct { int a; } schedule_t; typedef struct { int b; schedule_t schedule[6]; } student_t; int main() { system("pause"); }

9 Chapter (14)

10 What’s going on inside the computer’s memory [http://computer.howstuffworks.com/c28.htm]

11 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

12 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

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

14 Each program uses several “types” of memory program stack shared library objects heap Some of these are read only, others grow dynamically on as needed basis.

15 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

16 Process memory layout on an x86. [http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_processmemory.jpg]

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

18 Organization of a program’s memory [http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_stack1.jpg]

19 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]

20 Shared-library Memory [http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_library.jpg]

21 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]

22 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.”malloc()realloc()free()

23 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.

24 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) );

25 How malloc(..) works [http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_heap1.jpg]

26 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]

27 The free function Syntax: void free ( void * ptr ); Example: int* ip; ip = (int*) malloc( sizeof(int) ); free (ip);

28 Linked Lists

29 What is the meaning of this? typedef struct node { int value; struct node *next; } node_t; valuenext ?

30 Example: Dynamically Allocated Struct #include typedef struct node { int value; struct node *next; } node_t; int main() { // pointer to a dynamically allocated struct node_t *q = (node_t*) malloc (sizeof(node_t)); q->value = 10; q->next = NULL; printf("value = %d\n", q->value); system("pause"); }

31 What is the meaning of this code? #include 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; }

32 Singly-Linked List [http://en.wikipedia.org/wiki/Linked_list]

33 Inserting a node into a Linked List BeforeAfter [http://en.wikipedia.org/wiki/Linked_list]

34 Deleting a node from a linked list Before: After: [http://en.wikipedia.org/wiki/Linked_list]

35 Doubly-Linked List [http://en.wikipedia.org/wiki/Linked_list]

36 The node struct has 2 pointers typedef struct { int value; node_t next; node_t prev; } node_t;

37 Inserting a node into a doubly-linked list BeforeAfter [http://en.wikipedia.org/wiki/Linked_list]

38 Circularly-linked list [http://en.wikipedia.org/wiki/Linked_list]

39 Algorithm animations http://www.cosc.canterbury.ac.nz/mukundan/dsal/appldsal.html http://www.ansatt.hig.no/frodeh/algmet/animate.html

40 THE END


Download ppt "Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)"

Similar presentations


Ads by Google