Transcript Document

CprE 185:
Intro to Problem Solving
(using C)
Instructor: Alexander Stoytchev
http://www.ece.iastate.edu/~alexs/classes/2009_Fall_185/
Dynamic Data Structures
CprE 185: Intro to Problem Solving
Iowa State University, Ames, IA
Copyright © Alexander Stoytchev
Administrative Stuff
• There will be labs this week (also TA evaluations)
• This Wednesday we’ll have a review for the final
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.
Final Format
•
•
•
•
•
•
•
•
True/False
Fun Stuff
Short answers
Code Snippets
What is the output
Program 1
(15p)
Program 2
(20p)
Program 3
(20p)
• TOTAL
(130p)
(10 x 1p each
( 3 x 5p each
( 5 x 3p each
( 4 x 5p each
( 3 x 5p each
= 10p)
= 15p)
= 15p)
= 20p)
= 15p)
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
How do I 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");
}
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 uses
several “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
“Heap memory represents the dynamic memory used by
programs at runtime. Typically, processes allocate this
memory using the malloc(), realloc(), and free() functions.”
[http://www.qnx.com/developers/docs/6.3.0SP3/ide_en/user_guide/images/introduction_mema_heap2.jpg]
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 struct
node_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