Thread: Dynamic array - malloc

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    70

    Dynamic array - malloc

    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).
    */

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would say the init function should return a pointer so you can use it like this:
    Code:
    bufferqueue *init_buffer(int max, int min); /* func prototype */
    bufferqueue *example = init_buffer(30, 5);   /* example call */
    Does that give you a clue? The destructor would then accept a bufferqueue* as a parameter.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    Should i use max - min to allocate memory for the buffer array?

    If so how would this be implemented, i am having difficulty getting my head around the manual for malloc

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by erasm View Post
    Should i use max - min to allocate memory for the buffer array?

    If so how would this be implemented, i am having difficulty getting my head around the manual for malloc
    Malloc is fairly simple:
    Code:
    bufferqueue *init_buffer(int max, int min) {
             bufferqueue *ptr = malloc(sizeof(bufferqueue));
             ptr->buffer_array = malloc(max*sizeof(int));
             [....]
             return ptr;
    }
    The most important thing about using malloc is understanding free(), and using it where appropriate to prevent wasted memory and memory leaks. In fact this is almost THE MOST fundamental aspect of C low level programming.

    In this case, free() is the essential element of your destructor.

    If your code needs to compile as C++, you need to cast malloc() calls. Otherwise that is not necessary.

    Some people will stress the importance of checking to make sure malloc returns a non-NULL pointer, but this is debatable. If you are submitting this for an assignment, make sure you know your prof's opinion.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    I am using a C++ tutorial as a reference on circular buffers and have a problem understanding the logic behind part of it.

    Shouldn't remove() return an array excluding the oldest element in the array?

    I don't see how this code from the tutorial is doing so.

    Code:
        void insert (Node node) {
            array[next] = node;
            next++;
        }
    
        Node remove () {
            first++;
            return array[first-1];
        }
    From: C++Course: Circular Buffer

    b->buffer_array[out] = NULL; <-- Would this be the best way to remove the array element?

    Code:
    #include "circbuffer.h"
    
    bufferQueue *init_buffer(int min, int max)
    {
            bufferQueue *b = malloc(sizeof(bufferQueue));
            b->buffer_array = malloc(max*sizeof(int));
            b->in = 0;
            b->out = 0;
            return b;
    }
    
    bufferQueue *destroy_buffer(bufferQueue *b)
    {
            free(b);
            return b;
    }
    
    void put_buffer(bufferQueue *b, int v)
    {
            b->buffer_array[b->in] = v;
            b->in++;
    }
    
    void get_buffer(bufferQueue *b)
    {
            //b->buffer_array[out] = NULL;
            b->out++;        
    }
    Last edited by erasm; 10-04-2009 at 06:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  2. Dynamic array help
    By Perogy in forum C Programming
    Replies: 27
    Last Post: 04-08-2009, 02:25 PM
  3. dynamic array malloc()
    By el_chupacabra in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 07:46 AM
  4. Checking maximum values for dynamic array...
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 03-21-2005, 12:39 AM
  5. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM