C语言程序中的内存结构数组

简介:

我个人的理解:其实质,和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.
 */
void
InitFileAccess(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 File
AllocateVfd(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;
}
复制代码





目录
相关文章
|
1天前
|
C语言
C语言之分支结构
C语言之分支结构
23 0
|
1天前
|
C语言
C语言结构体内存对齐
C语言结构体内存对齐
|
1天前
|
Serverless C语言
C语言程序通常具有以下基本结构
C语言程序通常具有以下基本结构
11 0
|
1天前
|
C语言
C语言选择结构
C语言选择结构
17 0
|
1天前
|
存储 C语言
C语言顺序结构:基础与实践
C语言,作为一种广泛使用的编程语言,其程序结构可以分为三种基本类型:顺序结构、选择结构和循环结构。在这篇文章中,我们将重点讨论顺序结构的概念、特点以及如何在C语言中实现顺序结构的程序设计。
44 1
|
1天前
|
C语言
C语言选择结构
C语言选择结构
17 0
|
1天前
|
存储 编译器 Linux
【C语言】自定义类型:结构体深入解析(二)结构体内存对齐&&宏offsetof计算偏移量&&结构体传参
【C语言】自定义类型:结构体深入解析(二)结构体内存对齐&&宏offsetof计算偏移量&&结构体传参
|
1天前
|
C语言
C语言最基本程序控制结构
C语言最基本程序控制结构
16 0
|
1天前
|
C语言
【精通C语言】:分支结构if语句的灵活运用
【精通C语言】:分支结构if语句的灵活运用
25 1
|
1天前
|
编译器 Linux C语言
C语言:结构体(自定义类型)知识点(包括结构体内存对齐的热门知识点)
C语言:结构体(自定义类型)知识点(包括结构体内存对齐的热门知识点)