Results 1 to 4 of 4

Thread: buffers... can u help?

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    buffers... can u help?

    This is for a friend of mine, who's too lazy to join a forum..

    How do you use a circular buffer to store an array of data?

    That's his question.
    Can you guys give some ideas on how to do this, I'll just do a copy and paste and printout and shove it in his face.

    thanks

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    implementing a queue maybe?
    Code:
    int buffer[10]
    buffer[(offset+X)%10]=1;
    ++offset;
    buffer[(offset+X)%10]=2;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    jim mcnamara
    Guest
    A circular buffer is a linked list with a slight twist.
    The 'first' element in the list points to the last as the previous element, and the last element points to the first element as the next. This code creates the buffer

    Code:
    typedef struct x{
       struct x *prev;
       char data[100]; 
       struct x *next; 
    };
    struct x m[100];
    
    for (i=1;i<100;i++){
         m[i].prev = &m[i-1];
         m[i].next = &m[i+1];
         memset(m[i].data,0x00,sizeof(m[i].data));
    }
    m[0].next=&m[1];
    m[0].prev=&m[100];
    m[100].next=&m[0];
    m[100].prev=&m[99];
    memset(m[0].data,0x00,sizeof(m[0].data));
    memset(m[100].data,0x00,sizeof(m[100].data));

  4. #4

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Surprisingly, I understood that myself.

    OK, Thanks. just what he needed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width