tags:

views:

137

answers:

4

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.

+3  A: 

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
}
Williham Totland
this is is correct I had to create a new buffer each time in the array.
how do I initialize each buffer to zero? I get the following error:functions.h:28: warning: assignment makes integer from pointer without a castso when I try to print each element I get a segmentation fault
calloc zeroes out the memory allocated
jmucchiello
A: 

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 :-(

Nir Levy
A: 

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 :)

Mark
A: 

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 };
Joel Baker
I am trying to create an array of buffers. All these buffers will act as shared memory among different processes. Each process will read and write an integer to the buffers. So basically I need an array of buffers, and stored the integer zero in each buffer initially. I keep getting this error:assignment makes integer from pointer without a cast
that would be at this linebufferArr[i] = (int *) calloc(BUFFER_LENGTH, sizeof(int));
i changed (int *) to (int) and i do not get the error anymore. However I still get a segmentation fault when I tried to read from the buffers.
Make sure you've declared the buffer variable as a double-pointer (int**) instead of just a single. The error you're seeing is likely due to this, since changing the cast fixes the compile error.
Joel Baker