1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include<stdio.h> #include<stdlib.h> int chunk_time =0; int chunk_size[50]; char *chunk_ptr[50]; void init(){ setvbuf(stdout, 0, 2, 0); setvbuf(stdin, 0, 2, 0); } void menu(){ puts("what u want to do next?"); puts("1. Build a house"); puts("2. Delete a house"); puts("3. Edit your house"); puts("4. Show your house"); puts(">"); } void add(){ char size[20]; if(chunk_time<=32&&chunk_time>=0){ if(!chunk_ptr[chunk_time]){ printf("You have built %d houses in my world\n",chunk_time); puts("You can customize the size of house here, but what about your life"); read(0,size,0x8); chunk_size[chunk_time] = atoi(size); chunk_ptr[chunk_time] = malloc(chunk_size[chunk_time]); puts("add something to your house"); read(0,chunk_ptr[chunk_time],chunk_size[chunk_time]); chunk_time++; }else{ puts("something wrong,but you can come in any time"); exit(0); } }else{ puts("something wrong,but you can come in any time"); exit(0); } } void delete(){ int index; puts("I won't set the pointer to zero"); puts("every decision you made is meaningful"); scanf("%d",&index); free(chunk_ptr[index]); } void edit(){ int index; puts("something wrong?"); puts("It's never too late to make changes"); scanf("%d",&index); puts("something interesting here"); read(0,&chunk_size[index],0x8); puts("Nice choice!"); puts("Now add something"); read(0,chunk_ptr[index],chunk_size[index]); puts("ok\n"); } void show(){ puts("Nothing is perfect,but you can make something different"); puts("Let's see what you can do"); int index; scanf("%d",&index); puts(chunk_ptr[index]); }
int main(){ int choice; init(); puts("This program is used to explore something about heap"); puts("written by str1k3"); puts("Welcome to my world!"); puts("In my world,you can build up some houses"); while(1){ menu(); scanf("%d",&choice); switch(choice){ case 1: add(); break; case 2: delete(); break; case 3: edit(); break; case 4: show(); break; case 5: puts("ready to exit?"); puts("back to your life!"); exit(0); break; } } }
|