今天重新看了《STL源代码剖析》,不禁要赞叹STL设计的经典。STL 的空间适配代码设计的尤为精辟,不仅考虑到内存碎片的隐患,而且考虑到指针空间的节俭和复用,降低维护链表(lists)带来额外的负担。我们来看看如下代码;
先看看STL的结构体,
union obj{ union obj *free_list_link; char clent_data[1]; /* the client sees this */ };
obj 之所以用union,由于union之故,从其第一个字段观之,obj可被视为一个指针,指向相同形式的另一个obj。从其第二个字段观之,obj可被视为一个指针,指向实际区域。一物二用的结果是,不会为了维护链表所必须的指针而造成内存的另一种让费。
看看如下示例代码,你就发现它的设计精妙之处!
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> using namespace std; union obj{ union obj *free_list_link; char clent_data[1]; /* the client sees this */ }; int main() { obj *op1 = (obj *)malloc(32); strcpy(op1->clent_data,"hello free!"); obj *op2 = (obj *)malloc(32); op2->free_list_link = NULL; obj *op3 = (obj *)malloc(32); strcpy(op3->clent_data,"hello free l"); printf("----------------------------------------\r\n"); printf("address freelist:%ld\r\n",(long)(op3->free_list_link)); printf("address clentdata:%ld\r\n",(long)&(op3->clent_data)); obj *op4 = (obj *)malloc(32); op4->free_list_link = op2; obj *op5 = (obj *)malloc(32); op5->free_list_link = op4; printf("----------------------------------------\r\n"); obj *begin = op5; while(begin){ printf("%ld\r\n",(long)begin); begin = begin->free_list_link; } /* release op3 */ op2->free_list_link = op3; op3->free_list_link = NULL; printf("----------------------------------------\r\n"); printf("address freelist:%ld\r\n",(long)(op3->free_list_link)); printf("address clentdata:%ld\r\n",(long)&(op3->clent_data)); printf("----------------------------------------\r\n"); begin = op5; while(begin){ printf("begin:%ld\r\n",(long)begin); begin = begin->free_list_link; } return 0; }
输出如下:
----------------------------------------
address freelist:1819043176
address clentdata:143143000
----------------------------------------
143143080
143143040
143142960
----------------------------------------
address freelist:0
address clentdata:143143000
----------------------------------------
begin:143143080
begin:143143040
begin:143142960
begin:143143000