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

  • Thread starter Thread starter Rocket254
  • Start date Start date
  • Tags Tags
    Circular Pointers
Click For Summary
SUMMARY

This discussion focuses on implementing a dynamic circular chasing pointer array of integers using C programming. The proposed solution utilizes a fixed-size array with a maximum capacity of 1024 integers, employing pointers for efficient element insertion and retrieval. Key functions include PutElement, GetElement, and PeekElement, which manage the circular behavior of the array. Mutexes are recommended for multi-tasking scenarios to ensure thread safety during operations.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with pointer manipulation in C
  • Knowledge of circular buffer concepts
  • Basic understanding of multi-threading and mutex usage
NEXT STEPS
  • Study C programming memory management techniques
  • Learn about implementing circular buffers in C
  • Explore multi-threading in C with pthreads
  • Research mutex implementation for thread safety in C
USEFUL FOR

Software developers, particularly those working with C programming, embedded systems engineers, and anyone interested in implementing efficient data structures for real-time applications.

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:

Similar threads

Replies
5
Views
2K
Replies
12
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
11K
  • · Replies 20 ·
Replies
20
Views
6K
  • · Replies 16 ·
Replies
16
Views
5K