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

4 Final Exam Final Exam: Tuesday Dec 16: 9:45am- 11:45am Location: This room.

5 HW10: How to clear the screen? #include int main() { printf("%d\n", 34523); system("cls"); // system call system("pause"); }

6 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"); }

7 Pointers to Structures #include typedef struct node { int value; } node_t; int main() { node_t Node; Node.value = 5; // initialize it to 5 printf("value = %d\n", Node.value); // pointer to the statically allocated struct Node node_t *p = &Node; p->value = 6; // change it t0 6 printf("value = %d\n", p->value); }

8 Arrays of pointers #include int main() { int* pArray[10]; int a; int b; pArray[0] = &a; pArray[1] = &b; system("pause"); }

9 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 argument int *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’);

10 Example #include 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 fun2 funp(1,2); // an alternative way of calling a function using a pointer system("pause"); }

11 Chapter (14)

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

13 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

14 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

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

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

17 NOTE: The following slides are QNX specific but the same principles apply to almost any other modern operating system Source:

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

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

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

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

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

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

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

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

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

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

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

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

30 Linked Lists

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

32 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"); }

33 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; }

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

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

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

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

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

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

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

41 Algorithm animations

42 THE END


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

Similar presentations


Ads by Google