简体   繁体   English

C编程中的缓冲区数组?

[英]Array of buffers in C programming?

I am trying to create an array of buffers. 我正在尝试创建一个缓冲区数组。 I need to store an integer into each buffer. 我需要在每个缓冲区中存储一个整数。 I'm not quite sure how this should be done. 我不太确定该怎么做。

int BUFFER_LENGTH = 50;   //the size of each buffer
int numberOfBuffers = 10; //number of buffers
int *pBuffers;          //array of buffers

    pBuffers = (int *) calloc (numberOfBuffers, sizeof(int)); //make array size of numberOfBuffers

    int i;
    for (i = 0; i < n; i++){     //initialize each buffer to zero.
        &pBuffers[i] = 0x00;
  }

What is it that I am doing wrong? 我做错了什么? This code isn't really working. 这段代码实际上没有用。

You might want to allocate enough space. 您可能要分配足够的空间。 Right there you only allocate enough space for 10 ints; 在那儿,您只能为10个整数分配足够的空间。 looks like you want to allocate enough for 500. The simple way is int buffers[10][50] . 看起来您想为500分配足够的空间。简单的方法是int buffers[10][50] But if you want to calloc, you have to calloc(BUFFER_LENGTH, sizeof(int)) numberOfBuffers times. 但是,如果要调用calloc,则必须numberOfBuffers calloc(BUFFER_LENGTH, sizeof(int)) numberOfBuffers次。

Also, calloc automatically clears the allocated memory, so no need to do that. 另外,calloc会自动清除分配的内存,因此无需这样做。

#define BUFFER_LENGTH 50 /* the size of each buffer */
#define BUFFERS 10       /* number of buffers       */
int **pBuffers;          /* array of buffers        */

pBuffers = calloc (BUFFERS, sizeof(int *)); //make array of arrays
int i;
for (i = 0; i < BUFFERS; i++) {
  pBuffers[i] = calloc(BUFFER_LENGTH, sizeof(int)); // make actual arrays
}

What you're creating with your sample is an array of integers. 您使用样本创建的是一个整数数组。 Instead, you'll want to create an array of integer arrays. 相反,您将要创建一个整数数组的数组。 The setup is similar, but you'll need to declare the variable as an int** and allocate each buffer individually. 设置类似,但是您需要将变量声明为int**并分别分配每个缓冲区。

int **ppBuffer = (int**) calloc(numberOfBuffers, sizeof(int*));
for(int i = 0; i < numberOfBuffers; ++i)
    ppBuffer[i] = (int*) calloc(BUFFER_LENGTH, sizeof(int));

There's not much point in going through and initializing the arrays to 0 since calloc will already do that for you. 将数组初始化为0没有什么意义,因为calloc已经为您完成了。

Of course, the easier thing if you know the size of each buffer is going to be the constants would be to put it on the stack (and change you're int sizes to constants): 当然,如果您知道每个缓冲区的大小将是常量,那么比较容易的事情就是将其放在堆栈上(然后将int大小更改为常量):

int ppBuffer[numberOfBuffers][BUFFER_LENGTH] = { 0 };

I am very rusty in C, but I think you should change 我对C很生锈,但是我认为您应该改变

 &pBuffers[i] = 0x00;

to

 pBuffers[i] = 0x00;

(The [i] means you are already accessing the item in location i so there is no need to add the & .) ([i]表示您已经在位置i处访问该项目,因此无需添加& 。)

But I might be wrong :-( 但是我可能是错的:-(

I think what your asking is for an array of arrays, which isn't what your doing here in the code, rather you've created one array. 我认为您要的是数组数组,这不是您在代码中所做的,而是创建了一个数组。

Try something like: 尝试类似:

#define BUFFER_LENGTH 50
#define numberOfBuffers 10

int** pBuffers;

pBuffers = (int**) calloc(numberOfBuffers, sizeof(int*));

for (int i = 0; i < numberOfBuffers; i++)
    pBuffers[i] = (int*) calloc(BUFFER_LENGTH, sizeof(int));

As others said calloc initializes to 0 for you so you don't need to repeat that work. 正如其他人所说,calloc为您初始化为0,因此您无需重复该工作。 You may need to define int i outside the for loop depending on the version of C you're using. 您可能需要根据要使用的C版本在for循环外定义int i。

There are some other things that can be said about style and naming conventions but one step at a time :) 关于样式和命名约定,还可以说其他一些事情,但一次只能做一个步骤:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM