English 中文(简体)
C区巡回图书馆设计
原标题:Reentrant library design in C

请允许我说,Im 建造一个图书馆,在C区进行间歇。

质量需要两个国家变量,才能成功推广:

static int quux_state;
static char* quux_address;

/* function to spork quuxes found in a file, 
   reads a line from the file each time it s called. */
void spork_quux(FILE*);

如果我把这一数据作为全球变量加以储存,那么只有一位客户能够在某个时候进行间歇,否则国家变量就会被第二名电线员操纵,而灾难可能随之发生。

问题是,在C设计巡回图书馆的最佳方式是什么?

我审理了以下案件,但没有得出令人满意的结论。

在以下情况下,问题是如何将客户与每个国家联系起来?

/* library handles all state data allocation */
static int* quux_state; 
static char** quux_address;

在以下情况下,客户能够向国家传递信息,这是不受欢迎的。

/* let each client store state */
typedef struct { int state; char* address; } QuuxState; 
QuuxState spork_quux(FILE*);

因此,如何适当做到这一点?

最佳回答

使用结构,但不会使定义暴露于客户。

比如,在.首档案中:

typedef struct QuuxState QuuxState;

QuuxState *spork_quux(FILE*);

以及执行档案:

struct QuuxState
{
    int state;
    char* address;
};

QuuxState *spork_quuxFILE *f)
{
    QuuxState *quuxState = calloc(1, sizeof(*quuxState));
    if (!quuxState)
        return NULL;

    quuxState->state = ....;

    ........

    return quuxState;
}

这种做法的好处是:

  1. you can change the contents of the struct without recompiling all client code
  2. the client code cannot access the members, ie. quuxState->state would give a compiler error
  3. the QuuxState structure will still be fully visible to the debugger, so you can see the values trivially and set watchpoints, etc.
  4. no casting necessary
  5. the type you re returning is a specific type, so you will get some compiler checking that the correct thing is being passed (compared to a void* pointer)

唯一的不利之处是,你必须分配一个记忆圈——然而,假定你的图书馆正在做任何并非微不足道的事情(如果它做的是I/O,那肯定是非自然的),一个小块的间接费用是微不足道的。

你不妨将上述职能改名为QuuxSpork_create,并增加更多职能,处理逐条工作,并在你重新工作后摧毁国家。

void QuuxSpork_readLine(QuuxState *state)
{
    ....
}

void QuuxSpork_destroy(QuuxState *state)
{
    free(state);
}

一个大致如此运作的图书馆的随机例子,是PPOSIX翻新图书馆和校对。

问题回答

使用一种结构,但并不告诉客户它是一种障碍。 穿透视器——真空*,或更贴上空洞的 du点,必要时将其 back回。

图书馆大部分职能处理的方式是,在用户需要的任何数据类型中向用户提供国家信息。 就你而言,是个障碍。 (take strtok vs strtok_r). 我认为,这开创了一种先例,你应将它退回用户。 * 无效。 你们甚至可以打字,这样就能够看上去。

此外,通过编辑指挥线论点,鲁特克-鲁克(r)这样做,而不是把一个点退回到国家。 我期望任何重复性功能,即使用类似形式。 当然,我大脑被一些粗金刚cra的C法典打了。

void spork_quux(FILE*, QuuxState **);




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签