I would like to implement a dynamic array buffer_array for this tutorial. A hint for the tutorial suggests using malloc. How can malloc be used to implement this dynamic array for my tutorial?
Code:
/*
Create a data type (e.g. a struct) for the buffer
(queue) with an appropriate constructor (intialiser) and
destructor (cleanup function). The constructor should
take at least two (integer) parameters: a (maximum)
buffer size, and a minimum fill level.
*/
typedef struct bufferQueue
{
int *buffer_array;
int min_buffer_size;
int max_buffer_size;
} bufferQueue;
void init_bufferQueue(int min, int max)
{
}
destroy_bufferQueue
{
}
/*
Create a put_buffer() function that puts an
integer into the buffer so that it can be read later.
For the moment, you can ignore the maximum buffer size.
*/
/*
Create a get_buffer() function that reads the
oldest integer that was put into the buffer, removes that
integer from the buffer and returns it.
For the moment, you can ignore the minimum fill level.
*/
/*
Create a test program that spawns a child thread
that acts as the producer. I.e. the child should
use the function you created earlier to read integers from
/dev/random as fast as it can and immediately put each integer into the
buffer. The main (parent) thread should act as the consumer
(i.e. read and remove the numbers
from the buffer, and print them to stdout).
*/