博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C语言程序中的内存结构数组
阅读量:6945 次
发布时间:2019-06-27

本文共 2604 字,大约阅读时间需要 8 分钟。

我个人的理解:其实质,和Java里的Hash表有点类似。在C语言中是为了解决数组无法扩展的缺陷。

例子:

看 PostgreSQL对 VFD的处理:

初始化:

/* * Virtual File Descriptor array pointer and size.    This grows as * needed.    'File' values are indexes into this array. * Note that VfdCache[0] is not a usable VFD, just a list header. */static Vfd *VfdCache;static Size SizeVfdCache = 0;
/* * InitFileAccess --- initialize this module during backend startup * * This is called during either normal or standalone backend start. * It is *not* called in the postmaster. */voidInitFileAccess(void){    Assert(SizeVfdCache == 0);    /* call me only once */    /* initialize cache header entry */    ///####    VfdCache = (Vfd *) malloc(sizeof(Vfd));    if (VfdCache == NULL)        ereport(FATAL,                (errcode(ERRCODE_OUT_OF_MEMORY),                 errmsg("out of memory")));    MemSet((char *) &(VfdCache[0]), 0, sizeof(Vfd));    VfdCache->fd = VFD_CLOSED;    SizeVfdCache = 1;    /* register proc-exit hook to ensure temp files are dropped at exit */    on_proc_exit(AtProcExit_Files, 0);}

扩展:

static FileAllocateVfd(void){    Index        i;    File        file;    DO_DB(elog(LOG, "AllocateVfd. Size %lu", SizeVfdCache));    Assert(SizeVfdCache > 0);    /* InitFileAccess not called? */    if (VfdCache[0].nextFree == 0)    {        /*         * The free list is empty so it is time to increase the size of the         * array.  We choose to double it each time this happens. However,         * there's not much point in starting *real* small.         */        Size        newCacheSize = SizeVfdCache * 2;        Vfd           *newVfdCache;        if (newCacheSize < 32)            newCacheSize = 32;        ////####        /*         * Be careful not to clobber VfdCache ptr if realloc fails.         */        newVfdCache = (Vfd *) realloc(VfdCache, sizeof(Vfd) * newCacheSize);        if (newVfdCache == NULL)            ereport(ERROR,                    (errcode(ERRCODE_OUT_OF_MEMORY),                     errmsg("out of memory")));        VfdCache = newVfdCache;        /*         * Initialize the new entries and link them into the free list.         */        for (i = SizeVfdCache; i < newCacheSize; i++)        {            MemSet((char *) &(VfdCache[i]), 0, sizeof(Vfd));            VfdCache[i].nextFree = i + 1;            VfdCache[i].fd = VFD_CLOSED;        }        VfdCache[newCacheSize - 1].nextFree = 0;        VfdCache[0].nextFree = SizeVfdCache;        /*         * Record the new size         */        SizeVfdCache = newCacheSize;    }    file = VfdCache[0].nextFree;    VfdCache[0].nextFree = VfdCache[file].nextFree;    return file;}

转载地址:http://ysonl.baihongyu.com/

你可能感兴趣的文章
tomcat的最大线程数、最大排队数多少合适
查看>>
安装snmp无法安装依赖包net-snmp-libs = 1:5.3.2.2-20.el5
查看>>
JVisualVM之GC插件+错误"not supported for this jvm"+命令jstatd
查看>>
wp7上启动器使用方法
查看>>
基于虚拟用户的邮件系统
查看>>
TODO:火热的集福,我这样看
查看>>
Linux进程和任务计划管理
查看>>
fsck修复命令
查看>>
活动目录管理的四个复杂性
查看>>
A Tale of Five Editors 之Wily
查看>>
Python Django shell 调试
查看>>
Vim 实用技术常用插件
查看>>
RHEL-用户基础-重定向
查看>>
awstats 安装备忘
查看>>
使用CocoaLumberjack的一些问题记录
查看>>
Nginx 服务控制脚本
查看>>
java.lang.NullPointerException pstmt = conn.prepareStatement(sql)报错
查看>>
《编写高质量代码 Web前端开发修炼之道》 - 书摘精要
查看>>
重视细节,方能得到认可
查看>>
科普:互联网的盈利模式
查看>>