no I need a buffer for 1 integer value only
Main Topics
Browse All TopicsHi I'm having some problems with my code here. Here is what I need to do.
1. Create an array of buffers of size nBuffers
2. Fill up the array with new buffers
3. Initialize each buffer to zero
Note: each buffer needs to hold an integer. (that's all, nothing else).
Problems:
1. Is this how everything should be done?
2. I get a segmentation fault when I try to print the values of the array
3. BUFFER_SIZE is set to 50. Should it just be 1 since I'm only storing an integer in each buffer?
#define BUFFER_LENGTH 50 //size of buffer
int nBuffers; //the number of buffers to be allocated.
int *bufferArr; //pointer to future array of buffers
bufferArr = calloc (nBuffers, sizeof(int *));
int i;
for (i = 0; i < nBuffers; i++){
bufferArr[i] = (int) calloc(BUFFER_LENGTH, sizeof(int));
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
> bufferArr[i] = (int) calloc(BUFFER_LENGTH, sizeof(int));
This line is wrong. You are allocating dynamic memory and casting the returned address to integer value (which will discard part of the address on many architectures).
If you need just 1 integer buffers, then there is absolutely no need to allocate them, just create an array of ints with the length nBuffers. If nBuffers is a variable you will need to use vector<int> or a dynamic array in C++.
A TUTORIAL ON POINTERS AND ARRAYS IN C
calloc: Allocates a block of memory for an array of num elements, each of them size bytes long, and initializes all its bits to zero.
>> here's the complete one with the zero initialization also done
If you want to zero-initialize the memory, why not call calloc instead of malloc ?
Note however that zero-initializing memory doesn't guarantee that the all data will have the value 0. so you might be better off with a loop that sets each data value to 0 separately.
That was not my point. memset is meant for zero-initializing a block of memory (and you might as well use calloc, because it already does that internally), but that doesn't guarantee that the data values in that block of memory will also actually have the values 0. For int's it's likely, for other data types less so.
Business Accounts
Answer for Membership
by: pgnatyukPosted on 2009-11-01 at 14:54:48ID: 25716021
You mean you need a buffer for 50 integer values?
* sizeof(int));
int* bufferArr;
bufferArr = (int*)malloc(BUFFER_LENGTH
memset(bufferArr, 0, BUFFER_LENGTH * sizeof(int));
it is so.