How to Implement a Dynamic Circular Chasing Pointer Array of Integers?

  • Thread starter Thread starter Rocket254
  • Start date Start date
  • Tags Tags
    Circular Pointers
AI Thread Summary
The discussion revolves around implementing a dynamic circular chasing pointer array for integers, commonly used for creating a circular FIFO (First In, First Out) structure. Key points include the suggestion to use pointers for efficient indexing, particularly on Intel CPUs, while noting potential overhead on other architectures. The implementation involves defining a maximum size for the array, initializing pointers for insertion (pPut) and retrieval (pGet), and managing the number of elements (iNumElements). Functions are provided for adding (PutElement), retrieving (GetElement), and peeking (PeekElement) at elements in the array. The importance of using a mutex for multi-tasking scenarios is highlighted, along with the option to omit overflow checking if not necessary. The code ensures that the pointers wrap around when reaching the end of the array, maintaining the circular nature of the structure.
Rocket254
Messages
33
Reaction score
0
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
 
Technology news on Phys.org
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:
// 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
        return;
    *pPut++ = iElement
    if(pPut >= pEnd)
        pPut = pBase;
    iNumElements += 1;
    return(iNumElements);
}

int GetElement(void)
{
int iValue;

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

int PeekElement(void)
{
    if(iNumElements == 0)    // return 0 if empty array, calling code should do this check
        return(0);
    return(*pGet);
}
 
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top