Instructor: Alexander Stoytchev http://www.cs.iastate.edu/~alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)
Dec 16, 2015
Instructor: Alexander Stoytchev
http://www.cs.iastate.edu/~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
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
Final Exam
• Final Exam: Tuesday Dec 16: 9:45am- 11:45am
• Location: This room.
HW10: How to clear the screen?
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("%d\n", 34523);
system("cls"); // system call
system("pause");
}
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");}
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);
}
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");}
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’);
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");}
Chapter (14)
What’s going on inside the computer’s memory
[http://computer.howstuffworks.com/c28.htm]
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
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
[http://msdn.microsoft.com/en-us/library/ms810603.aspx]
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.
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
Process memory layout on an x86.
[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_processmemory.jpg]
Program Memory
[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_program.jpg]
Organization of a program’s memory
[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_stack1.jpg]
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]
Shared-library Memory
[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_library.jpg]
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]
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.”
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.
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) );
How malloc(..) works
[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_heap1.jpg]
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]
The free function
• Syntax:
void free ( void * ptr );
• Example:int* ip;
ip = (int*) malloc( sizeof(int) );
free (ip);
Linked Lists
What is the meaning of this?
typedef struct node
{
int value;
struct node *next;
} node_t;
value next?
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");}
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;
}
Singly-Linked List
[http://en.wikipedia.org/wiki/Linked_list]
Inserting a node into a Linked List
Before After
[http://en.wikipedia.org/wiki/Linked_list]
Deleting a node from a linked list
Before:
After:
[http://en.wikipedia.org/wiki/Linked_list]
Doubly-Linked List
[http://en.wikipedia.org/wiki/Linked_list]
The node struct has 2 pointers
typedef struct
{
int value;
node_t next;
node_t prev;
} node_t;
Inserting a node into a doubly-linked list
Before After
[http://en.wikipedia.org/wiki/Linked_list]
Circularly-linked list
[http://en.wikipedia.org/wiki/Linked_list]
Algorithm animations
• http://www.cosc.canterbury.ac.nz/mukundan/dsal/appldsal.html
• http://www.ansatt.hig.no/frodeh/algmet/animate.html
THE END