1. 首页
  2. 编程语言
  3. C
  4. C语言高级编程及实例部.rar

C语言高级编程及实例部.rar

上传者: 2018-12-28 11:21:19上传 RAR文件 641.68KB 热度 43次
例1:本节给出三个非常实用和高速的内存分配函数。读者可以直接拷贝这三个函数到程序中去,使用Allocate()和My_Free()代替系统提供的alloc()和free()函数。调试环境为TC2.0或者TC3.0 /* FILE: ALLOCATE.C */ #define PRIVATE static /* Used to hide identifiers from BIND */ #define BLKSIZ 80 /* Size of allocation block */ #define STKSIZ 1000 /* Bytes reserved for stack */ #define MLCSIZ 3000 /* Bytes reserved for malloc() */ typedef union { char *link ; char data[BLKSIZ] ; } BLOCK ; PRIVATE char *btm_of_heap ; PRIVATE char *top_of_heap ; PRIVATE BLOCK *heap ; void Init_Heap() { unsigned *ptr ; unsigned size ; unsigned blk ; unsigned ttl_blks ; freeall(STKSIZ) ; /* Reserve a stack of 500 words */ ptr = _memory() + 1 ; /* Get pointer to heap size */ size = *ptr ; /* Get size of heap */ size -= MLCSIZ ; /* Keep 3K for malloc() */ ttl_blks = size/sizeof(BLOCK) ; /* # available blocks */ size = sizeof(BLOCK)*ttl_blks ; /* Use an even multiple */ btm_of_heap = malloc(size) ; /* Reserve the blocks */ heap = btm_of_heap ; /* Form the free list */ for (blk = 0 ; blk < ttl_blks - 1; blk++) { heap->link = heap + 1 ; heap++ ; } heap->link = 0 ; /* Establish end marker */ heap = btm_of_heap ; /* Restore head pointer */ top_of_heap = &heap[ttl_blks] ; /* 1st malloc() block */ } void My_Free(ptr) BLOCK *ptr ; { if (ptr >= btm_of_heap) { if (ptr < top_of_heap) /* Block is from my heap */ { ptr->link = heap ; heap = ptr ; return ; } else if (free(ptr)) /* Block is from C-Ware's heap */ { return ; } } puts("\nAttempt to free unallocated block!\n\7") ; exit(1) ; } char *Allocate(bytes) unsigned bytes ; { BLOCK *ptr ; if (bytes link ; return ptr ; } } if (ptr = malloc(bytes)) /* Won't fit or none left! */ { return ptr ; } puts("Insufficient memory: malloc()\n\7") ; exit(1) ; } ; unsigned size ; unsigned blk ; unsigned ttl_blks ; freeall(STKSIZ) ; /* Reserve a stack of 500 words */ ptr = _memory() + 1 ; /* Get pointer to heap size */ size = *ptr ; /* Get size of heap */ size -= MLCSIZ ; /* Keep 3K for malloc() */ ttl_blks = size/sizeof(BLOCK) ; /* # available blocks */ size = sizeof(BLOCK)*ttl_blks ; /* Use an even multiple */ btm_of_heap = malloc(size) ; /* Reserve the blocks */ heap = btm_of_heap ; /* Form the free list */ for (blk = 0 ; blk < ttl_blks - 1; blk++) { heap->link = heap + 1 ; heap++ ; } heap->link = 0 ; /* Establish end marker */ heap = btm_of_heap ; /* Restore head pointer */ top_of_heap = &heap[ttl_blks] ; /* 1st malloc() block */ } void My_Free(ptr) BLOCK *ptr ; { if (ptr >= btm_of_heap) { if (ptr < top_of_heap) /* Block is from my heap */ { ptr->link = heap ; heap = ptr ; return ; } else if (free(ptr)) /* Block is from C-Ware's heap */ { return ; } } puts("\nAttempt to free unallocated block!\n\7") ; exit(1) ; } char *Allocate(bytes) unsigned bytes ; { BLOCK *ptr ; if (bytes link ; return ptr ; } } if (ptr = malloc(bytes)) /* Won't fit or none left! */ { return ptr ; } puts("Insufficient memory: malloc()\n\7") ; exit(1) ; }
用户评论