如何在C语言库中初始化数组大小? [英] How to initialize array size in a library in C?

查看:202
本文介绍了如何在C语言库中初始化数组大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.h和.c文件为环形缓冲区创建C库.理想情况下,您可以在主项目中使用 ringbuff_init(int buff_size); 之类的东西来初始化此环形缓冲区库,并且发送的大小将是缓冲区的大小.当需要静态初始化C中的数组时,该怎么办?

I'm creating a C-library with .h and .c files for a ring buffer. Ideally, you would initialize this ring buffer library in the main project with something like ringbuff_init(int buff_size); and the size that is sent, will be the size of the buffer. How can I do this when arrays in C needs to be initialized statically?

我已经尝试了一些动态分配数组的方法,但没有使它起作用.当然可以通过某种方式完成此任务吗?

I have tried some dynamically allocating of arrays already, I did not get it to work. Surely this task is possible somehow?

我想做的是这样的:

int buffSize[];

int main(void)
{
    ringbuffer_init(100);      // initialize buffer size to 100
}

void ringbuffer_init(int buff_size)
{ 
    buffSize[buff_size];
}

这显然不能编译,因为该数组应该已经在声明中初始化了.所以我的问题是,实际上,当您为缓冲区之类的东西创建库时,如何在主程序中初始化它(以便在缓冲区库的.h/.c文件中),将缓冲区大小设置为所需的大小大小?

This obviously doesn't compile because the array should have been initialized at the declaration. So my question is really, when you make a library for something like a buffer, how can you initialize it in the main program (so that in the .h/.c files of the buffer library) the buffer size is set to the wanted size?

推荐答案

您要使用动态内存分配.您最初尝试的直接翻译如下:

You want to use dynamic memory allocation. A direct translation of your initial attempt would look like this:

size_t buffSize;
int * buffer;

int main(void)
{
    ringbuffer_init(100);      // initialize buffer size to 100
}

void ringbuffer_init(size_t buff_size)
{ 
    buffSize = buff_size;
    buffer = malloc(buff_size * sizeof(int));
}

但是,这里的解决方案非常糟糕.让我在这里列出问题:

This solution here is however extremely bad. Let me list the problems here:

  1. 不检查malloc的结果.如果分配失败,它可能会返回NULL.
  2. 缓冲区的大小需要与缓冲区一起存储,否则无法从您的库代码中知道缓冲区的大小.保留这些全局变量并不完全干净.
  3. 就这些而言,这些全局变量绝对不是线程安全的.如果多个线程调用您库的函数,则结果是不可预测的.您可能希望将缓冲区及其大小存储在将从init函数返回的结构中.
  4. 没有什么可以阻止您连续多次调用init函数,这意味着缓冲区指针每次都将被覆盖,从而导致内存泄漏.
  5. 最终必须使用 free 函数释放已分配的内存.
  1. There is no check of the result of malloc. It could return NULL if the allocation fails.
  2. Buffer size needs to be stored along with the buffer, otherwise there's no way to know its size from your library code. It isn't exactly clean to keep these global variables around.
  3. Speaking of which, these global variables are absolutely not thread-safe. If several threads call functions of your library, results are inpredictible. You might want to store your buffer and its size in a struct that would be returned from your init function.
  4. Nothing keeps you from calling the init function several times in a row, meaning that the buffer pointer will be overwritten each time, causing memory leaks.
  5. Allocated memory must be eventually freed using the free function.

总而言之,您需要非常仔细地考虑您在库中公开的API,并且实现虽然不是非常复杂,但也不是简单的.

In conclusion, you need to think very carefully about the API you expose in your library, and the implementation while not extremely complicated, will not be trivial.

更正确的方法如下:

typedef struct {
    size_t buffSize;
    int * buffer;
} RingBuffer;

int ringbuffer_init(size_t buff_size, RingBuffer * buf)
{
    if (buf == NULL)
        return 0;

    buf.buffSize = buff_size;
    buf.buffer = malloc(buff_size * sizeof(int));

    return buf.buffer != NULL;
}

void ringbuffer_free(RingBuffer * buf)
{
    free(buf.buffer);
}

int main(void)
{
    RingBuffer buf;
    int ok = ringbuffer_init(100, &buf);      // initialize buffer size to 100

    // ...

    ringbuffer_free(&buf);
}

即使这也不是没有问题,因为如果对同一缓冲区多次调用init函数,仍然存在潜在的内存泄漏,并且您的库的客户端一定不要忘记调用free函数.

Even this is not without problems, as there is still a potential memory leak if the init function is called several times for the same buffer, and the client of your library must not forget to call the free function.

这篇关于如何在C语言库中初始化数组大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆