Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Circular chasing pointers

  1. Feb 29, 2008 #1
    Circular chasing pointers....

    Can anyone please point me towards a site that can explain how to implement a dynamic circular chasing pointer array of integers?

    Thanks in advance
  2. jcsd
  3. Feb 29, 2008 #2


    User Avatar
    Homework Helper

    I'm guessing here, but it sounds like you want to create a circular fifo using pointers (indexes could also be used, with no additional overhead on an Intel CPU, but possibly more overhead on other CPUs).

    Code (Text):

    // for multi-tasking, a common mutex should be used in these functions
    // if there's no need for overflow checking, iNumElements can be removed,
    //    and an empty array is indicated when pGet == pPut

    // if PutElement returns a 1, the array just went from empty to non-empty.

    #define MAXSIZE 1024 // up to 1024 integers

    static          int aIntegers[MAXSIZE];
    static constant int *pBase = &aIntegers[0];
    static constant int *pEnd  = &aIntegers[MAXSIZE];
    static          int *pPut  = &aIntegers[0];
    static          int *pGet  = &aIntegers[0];
    static          int iNumElements = 0;

    int PutElement(int iElement)
        if(iNumElements == MAXSIZE)    // prevent overflow, caller code should check this
        *pPut++ = iElement
        if(pPut >= pEnd)
            pPut = pBase;
        iNumElements += 1;

    int GetElement(void)
    int iValue;

        if(iNumElements == 0)    // return 0 if empty array, calling code should do this check
        iNumElements -= 1;
        iValue = *pGet++;
        if(pGet >= pEnd)
            pGet = pBase;

    int PeekElement(void)
        if(iNumElements == 0)    // return 0 if empty array, calling code should do this check

    Last edited: Feb 29, 2008
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Circular chasing pointers
  1. This pointer (Replies: 2)

  2. Pointers and classes (Replies: 1)

  3. Use of pointers (Replies: 3)

  4. Pointers and strings (Replies: 19)