开发者

Array of buffers in C programming?

开发者 https://www.devze.com 2022-12-10 10:12 出处:网络
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.

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 bu开发者_StackOverflow中文版ffers
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; looks like you want to allocate enough for 500. The simple way is int buffers[10][50]. But if you want to calloc, you have to calloc(BUFFER_LENGTH, sizeof(int)) numberOfBuffers times.

Also, calloc automatically clears the allocated memory, so no need to do that.

#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 **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.

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 ppBuffer[numberOfBuffers][BUFFER_LENGTH] = { 0 };


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. You may need to define int i outside the for loop depending on the version of C you're using.

There are some other things that can be said about style and naming conventions but one step at a time :)


I am very rusty in C, but I think you should change

 &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 &.)

But I might be wrong :-(

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号