Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- struct list_items
- {
- struct list_items *next;
- int *data;
- int size;
- };
- struct list_ptrs
- {
- struct list_ptrs *next;
- struct list_items *items;
- };
- static struct list_ptrs *spl = NULL;
- int *alloc_continue(int n)
- {
- printf("alloc_continue(%d)\n", n);
- struct list_ptrs *ptr = spl;
- struct list_items *item = malloc(sizeof(struct list_items));
- item->next = ptr->items;
- item->data = malloc(sizeof(int)*n);
- item->size = n;
- ptr->items = item;
- return item->data;
- }
- int *alloc_begin(int n)
- {
- printf("alloc_begin(%d)\n", n);
- struct list_ptrs *ptr = malloc(sizeof(struct list_ptrs));
- ptr->next = spl;
- ptr->items = NULL;
- spl = ptr;
- return alloc_continue(n);
- }
- void print_spl()
- {
- int l = 0;
- struct list_ptrs *ptr = spl;
- while(ptr)
- {
- printf("%d: ", l);
- struct list_items *item = ptr->items;
- while(item)
- {
- printf("%p[%d] ", item->data, item->size);
- item = item->next;
- }
- printf("\n");
- ptr = ptr->next;
- l++;
- }
- }
- void free_block()
- {
- struct list_ptrs *ptr = spl;
- if(!ptr)
- return;
- struct list_items *item = ptr->items;
- spl = ptr->next;
- free(ptr);
- printf("Delete: ");
- while(item)
- {
- printf("[%d] ", item->size);
- free(item->data);
- struct list_items *item_tmp = item;
- item = item->next;
- free(item_tmp);
- }
- printf("\n");
- }
- void foo()
- {
- int *a1 = alloc_begin(1);
- int *a2 = alloc_continue(2);
- int *a3 = alloc_continue(3);
- {
- int *a4 = alloc_begin(4);
- int *a5 = alloc_continue(5);
- print_spl();
- free_block();
- }
- free_block();
- }
- int main()
- {
- foo();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement