Circular Buffer

This is a discussion on Circular Buffer within the C Programming forums, part of the General Programming Boards category; I am writing a circular/ring buffer of a specified size. I am struggling with figuring out if my code is ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    8

    Circular Buffer

    I am writing a circular/ring buffer of a specified size. I am struggling with figuring out if my code is working. It compiles and runs with no problem, but I try testing it with print statements and I get confused. Can anyone give me some advice for how to test that my code is functioning properly - access up to a defined number of buffers and checks if the buffer is full or empty when requesting the next and freeing the last buffer.
    Last edited by parisframe; 09-17-2009 at 08:10 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For starters:
    Code:
    sb->dummy_val1==2;
    This doesn't do anything.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    I was trying to set a value to the dummy_val in my struct so I could test to see if numbers were be stored properly then to see if the code circled through it correctly. how do I do so?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Assignment: =
    Comparison: ==

    Compile with warnings on and it will tell you about that sort of thing.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    ok thanks, how can I test it now to see if it is working properly?

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    when running it I get "Bus error"

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. Why does incrementing the head always increase the count, but incrementing the tail always decreases the amount?

    2. It's generally a poor idea to name variables the same thing as functions. (See: increment_head)

    3. Your first call to 'requestNextBuffer()' is probably returning NULL, because it thinks the buffer is full, but then you go ahead and immediately assign something to that anyway.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    1. my thought process was that i needed to somehow keep track of how many parts of the buffer were full. So the incrementing up when allocating a buffer space, and decreasing when the buffer space is free would tell if the number of buffers ever reaches 16 (therefore being full).

    2.do you mean increment_head, and head?

    3. How can I assign something to the buffer then test if it is there and if it is, then test to see the program realizes that the buffer is full?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, I mean this:
    Code:
    int isBufferEmpty (int increment_head)
    {
    	if (increment_head <=0)
    Well, you need to set your buffer up right first. As it is, it's evaluating to full right at the start. Also, you don't seem to actually be using 'num_allocated' anywhere. You might consider using that to see if it's full or not. But, any time you use a pointer, it's best to actually see if it's NULL before you start trying to dereference it.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    oh oops, I meant that to be :

    Code:
    int isBufferEmpty (int num_allocated)
    {
    	if (num_allocated <=0)
    	{
    		return 1;
    	}
    I could have also used num_allocated for if the buffer was full (i.e. if(num_allocated >=16))

    Ok, so i am still a little hazy on how to check my buffer, does fixing the num_allocated in the bufferempty change how I am evaluating it?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    isfull
        if num allocated >= buffer size
            return full
        else
            return not full
    That's basically how you want to check for fullness. If you're using globals, you don't really need to pass it anything, provided you're remembering to actually increase the number allocated when you do allocate something.
    Code:
    getnew
        if not isfull
            return buffer[ currentposition ++ ]
        else
            return full
    Basically all you want to do is return the current spot, and update the current position. How you exactly do that is up to you, but it goes basically something like that.
    Code:
    freelast
        if not isempty
            return (buffer[ tail-- ] = 0)
        else
            return failure
    Similar to allocating, you probably want to return some indicator of success or failure. Not that you really need to return anything at all.

    It doesn't really matter what you call tail or currentposition, or whatever. You just need to make sure you update your actual variables correctly. I'm not sure why you even really need a circular buffer, if all you're doing is freeing the last thing allocated. I mean, it's not like you're ever going to go below zero, and it's not like you're going to ever go above the top.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Awesome, someone is actually asking about how they can unit test their code. Well I'm impressed!

    It looks like everyone else is helping you with the bugs they can see, rather than stating how it can be tested.
    Hmm, this would be so much easier if it were C++, but oh well...

    Before testing these functions, I would first correct the interface of isBufferEmpty and isBufferFull. It doesn't make sense to me that these would logically require any parameters. Well not the way you're doing it, and in C++ it would only require the implicit 'this' parameter. Then I'd start by testing those.
    The first test of each of these would be to check their result on an empty buffer. isBufferEmpty should return true whilst isBufferFull should return false. Note that I'm assuming that your buffer size will never be zero
    Then you try putting different numbers of items in there and check the results again. Unfortunately beyond this it becomes a little difficult because you're missing some kind of reset function. In C++ you'd just declare a new buffer in each test, but here you're effectively using the same "instance" every time. You'll therefore need some kind of reset function (which in itself kinda also needs testing).
    I'll help more once you get into it a bit more.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. DirectSound - multiple sounds
    By Magos in forum Game Programming
    Replies: 9
    Last Post: 03-03-2004, 03:33 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 04:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21