Line 8: | Line 8: | ||
+ | three ways to allocate memory: | ||
+ | 1. static---->know the size when writing the program | ||
+ | eg. int arr[100]; | ||
+ | vector v[200]; | ||
+ | 2. know the size somewhere during execution | ||
+ | eg. int length; | ||
+ | scanf("%d", &length); | ||
+ | int *arr; | ||
+ | arr = malloc(sizeof(int)*length); | ||
+ | 3.grow and shrink based on ran-time needs(dynamic structures) | ||
+ | eg. linked list, binary tree(list mode, tree mode) | ||
+ | |||
+ | type of struct dstructure | ||
+ | { | ||
+ | int value; | ||
+ | vector vec; | ||
+ | Person *p; | ||
+ | |||
+ | struct dstructure *next;(linked list) | ||
+ | struct dstructure * left;(binary tree) | ||
+ | struct dstructure * right;(binary tree) | ||
+ | } Node | ||
+ | |||
+ | streaming data | ||
+ | |||
+ | Node *n; | ||
+ | n = malloc(sizeof(Node)); | ||
+ | |||
+ | typeof struct listnode | ||
+ | { | ||
+ | int value; | ||
+ | struct listnode *next; | ||
+ | }Node;<---(don't forget) | ||
+ | |||
+ | (next few lectures, focus on linklist only, and data type would just be integer) | ||
+ | |||
+ | Node *n= NULL; | ||
+ | n = malloc(sizeof(Node)); | ||
+ | n->value = 264; | ||
+ | Node *n2; | ||
+ | n2 = malloc(sizeof(Node)); | ||
+ | n2->value = 2012; | ||
+ | n2->next = NULL; | ||
+ | n->next = n2; | ||
+ | |||
+ | (abstract view below) | ||
+ | assign one's value to another's link to link them together | ||
+ | |||
+ | Node *n3; | ||
+ | n3 = n; | ||
+ | printf("%d",n3->value);(=264) | ||
+ | n3 = n3->next; | ||
+ | printf("%d", n3->value);(=2012) | ||
+ | |||
+ | Node *Node.construct(int v) | ||
+ | { | ||
+ | Node *n; | ||
+ | n = mallloc(sizeof(Node)); | ||
+ | n->value = v; | ||
+ | n->next = NULL;(<----don't forget) | ||
+ | return n; | ||
+ | } | ||
+ | |||
+ | Node *List_insert(Node *t, int v) | ||
+ | { | ||
+ | Node *n = Node.construct(v); | ||
+ | n->next = t; | ||
+ | return n; | ||
+ | } | ||
+ | |||
+ | Node *head = NULL; | ||
+ | head = List.insert(head, 264); | ||
+ | head = List.insert(head, 2012); | ||
+ | |||
+ | Node *head = NULL; | ||
+ | head = List.insert(head, 264); | ||
+ | head = List.insert(head, 2012); | ||
[[ 2012 Spring ECE 264 Lu|Back to 2012 Spring ECE 264 Lu]] | [[ 2012 Spring ECE 264 Lu|Back to 2012 Spring ECE 264 Lu]] |
Revision as of 04:46, 22 March 2012
Lecture 19
Put your content here . . .
three ways to allocate memory:
1. static---->know the size when writing the program
eg. int arr[100];
vector v[200];
2. know the size somewhere during execution
eg. int length;
scanf("%d", &length);
int *arr;
arr = malloc(sizeof(int)*length);
3.grow and shrink based on ran-time needs(dynamic structures)
eg. linked list, binary tree(list mode, tree mode)
type of struct dstructure { int value; vector vec; Person *p;
struct dstructure *next;(linked list) struct dstructure * left;(binary tree) struct dstructure * right;(binary tree) } Node
streaming data
Node *n; n = malloc(sizeof(Node));
typeof struct listnode { int value; struct listnode *next; }Node;<---(don't forget)
(next few lectures, focus on linklist only, and data type would just be integer)
Node *n= NULL; n = malloc(sizeof(Node)); n->value = 264; Node *n2; n2 = malloc(sizeof(Node)); n2->value = 2012; n2->next = NULL; n->next = n2;
(abstract view below) assign one's value to another's link to link them together
Node *n3; n3 = n; printf("%d",n3->value);(=264) n3 = n3->next; printf("%d", n3->value);(=2012)
Node *Node.construct(int v) { Node *n; n = mallloc(sizeof(Node)); n->value = v; n->next = NULL;(<----don't forget) return n; }
Node *List_insert(Node *t, int v) { Node *n = Node.construct(v); n->next = t; return n; }
Node *head = NULL; head = List.insert(head, 264); head = List.insert(head, 2012);
Node *head = NULL; head = List.insert(head, 264); head = List.insert(head, 2012);