Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6222929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:24:02+00:00 2026-05-24T08:24:02+00:00

I am writing an embedded application designed to run at about 3-7 MHz so

  • 0

I am writing an embedded application designed to run at about 3-7 MHz so speed is CRITICAL. also the device only has 32K of RAM. Dynamic memory allocation is NOT and option. That said…

I am writing a buffering program which requires circular buffers with different queue lengths

  • 4x 1024+ int (+ meaning more than this is wanted if available)
  • 4x 256 int
  • 1x 256 int (with a different application than the one above)
  • 1x 2048+ int (+ meaning more than this is wanted if avaliable)

I have implemented it in the following code. You will note that I have created 2 of the buffers as an array of size 1, this is because I want to access all buffers using the same ReadBuff and WriteBuff functions. My problem is that when you pass a structure pointer to a function, the compiler expects that the pointers datatype is the same. Passing a pointer to a BuffType2 datatype to a function that is expecting a BuffType1, however, is somewhat illegal in C and may or may not work properly even though the structure of the structures is exactly the same with the exception of the queue size. The only way I can see to solve this problem is to dynamically allocate the size of my queue after I create the various structure arrays that I need.

It would also be good for you to know that I have designed the buffers to read from the tail and write to the head and it is designed such that when the head and tail indexes pass the buffer size it overflows to 0.

Here is a bit of code

/***************************************************************\
    Macro Definitions
\***************************************************************/ 
// The following sizes MUST be 2^n
#define BUFF_TYPE_1_SIZE    (0h400) /*  1024    */
#define BUFF_TYPE_2_SIZE    (0h100) /*  256 */
#define BUFF_TYPE_3_SIZE    (0h100) /*  256 */
#define BUFF_TYPE_4_SIZE    (0h800) /*  2048    */

#define BUFF_TYPE_1_Q_MASK  (BUFF_TYPE_1_SIZE-0h1)  /* all ones */
#define BUFF_TYPE_2_Q_MASK  (BUFF_TYPE_2_SIZE-0h1)  /* all ones */
#define BUFF_TYPE_3_Q_MASK  (BUFF_TYPE_3_SIZE-0h1)  /* all ones */
#define BUFF_TYPE_4_Q_MASK  (BUFF_TYPE_4_SIZE-0h1)  /* all ones */

//  Error Codes
#define ERROR_BUFF_EMPTY    (-1)    /*  The buffer is empty */
#define ERROR_BUFF_DNE      (-2)    /*  The buffer does not exist   */

//  Test for Buffer Empty
#define BUFF_EMPTY      (Buffer.Head == Buffer.Tail)

// Test for data in buffer
#define BUFF_NOT_EMPTY  (Buffer.Head != Buffer.Tail)

//  Test for Buffer Full
#define BUFF_FULL (((Buffer.Head + 1) & Buffer.Mask) == Buffer.Tail)

/***************************************************************\
    Structure Definitions
\***************************************************************/ 
// Buffers(queues) - These need to be global to allow use in interrupts
typedef struct BuffType1
    {
    int Head        = 0;
    int Tail        = 0;
    int Mask        = BUFF_TYPE_1_Q_MASK;
    char Full   = false;
    char Empty  = true;
    int Q[BUFF_TYPE_1_SIZE];
    };
typedef struct BuffType2
    {
    int Head        = 0;
    int Tail        = 0;
    int Mask        = BUFF_TYPE_2_Q_MASK;
    char Full   = false;
    char Empty  = true;
    int Q[BUFF_TYPE_2_SIZE];
    };
typedef struct BuffType3
    {
    int Head        = 0;
    int Tail        = 0;
    int Mask        = BUFF_TYPE_3_Q_MASK;
    char Full   = false;
    char Empty  = true;
    int Q[BUFF_TYPE_3_SIZE];
    };
typedef struct BuffType4
    {
    int Head        = 0;
    int Tail        = 0;
    int Mask        = BUFF_TYPE_4_Q_MASK;
    char Full   = false;
    char Empty  = true;
    int Q[BUFF_TYPE_4_SIZE];
    };

/***************************************************************\
    Global Variables
\***************************************************************/ 
// FIFO Ring buffers - These need to be global to allow use in interrupts
struct BuffType1 MyBuff1[4];    
struct BuffType2 MyBuff2[4];    
struct BuffType3 MyBuff3[1];    
struct BuffType4 MyBuff4[1];    

/***************************************************************\
    Functions
\***************************************************************/ 

/*---------------------------------------------------------------
int ReadBuff(struct BuffType1* BufferPtr)
Parameters  :   struct* BufferPtr
                        this is a pointer to the buffer you wish to read
Returns     :   int
                        if empty    - error code
                        if not empty - The value that was popped off the buffer
Description :   This function returns the value at the tail of the
                    buffer or an error if the buffer is empty. The 
                    tail is incremented after the read and overflows
                    automatically
---------------------------------------------------------------*/
int ReadBuff(struct BuffType1* BufferPtr)
    {
    int Value = ERROR_BUFF_EMPTY;   // error
    if(BUFF_EMPTY)
        (*BufferPtr).Empty = true;  // set the empty flag
    else
        {
        (*BufferPtr).Empty = false; // reset the empty flag
        Value = (*BufferPtr).Q[(*BufferPtr).Tail];  //  Read value is at the tail of the buffer
        (*BufferPtr).Tail = ((*BufferPtr).Tail + 1)&((*BufferPtr).Mask) /* increment the tail,
            making sure that if it rolls over its queue size, it rolls over to 0    */
        }
    return Value;
    }

/*---------------------------------------------------------------
int WriteBuff(struct* BufferPtr, int Data)
Parameters  :   struct* BufferPtr
                        The pointer to the buffer you wish to write to
                    int Data
                        The Data you wish to write to the buffer
Returns     :   true    - write was successful
                    false   - the buffer is full and did not write
Description :   This function writes the data to the head of the
                    buffer and returns an error if the buffer is full.
                    if the buffer is full, no data is written. The 
                    head is incremented after the write and overflows
                    automatically
---------------------------------------------------------------*/
char WriteBuff(struct BuffType1* BufferPtr, int Data)
    {
    int Success = false;    // there was an error writing to the buffer
    if (BUFF_FULL) 
        (*BufferPtr).Full = true;   // Indicate buffer is full (next avaliable spot to write is the tail)
    else
        {
        (*BufferPtr).Full = false;
        (*BufferPtr).Q[(*BufferPtr).Head] = Data;
        (*BufferPtr).Head = ((*BufferPtr).Head + 1)&((*BufferPtr).Mask)
        }
    return !((*BufferPtr).Full;);   // Return false if buffer was full and write could not happen
    }

/*---------------------------------------------------------------
void ResetBuff(struct* BufferPtr)
Parameters  :   struct* BufferPtr
                        The pointer to the buffer you wish to write to
Returns     :   nothing
Description :   This function resets the buffer but does not clear
                    anything
---------------------------------------------------------------*/
void ResetBuff(struct BuffType1* BufferPtr)
    {
    (*BufferPtr).Head = (*BufferPtr).Tail = 0;
    (*BufferPtr).Full = false;
    (*BufferPtr).Empty = true;
    (*BufferPtr).Q[0] = 0; //or null if it is defined
    }

/*---------------------------------------------------------------
void NullBuff(struct* BufferPtr)
Parameters  :   struct* BufferPtr
                        The pointer to the buffer you wish to write to
Returns     :   nothing
Description :   This function resets all values in the queue to 0
---------------------------------------------------------------*/
void NullBuff(struct BuffType1* BufferPtr)
    {
    int i;
    for(i=0; i=((*BufferPtr).Mask); i++) // for all values in the buffer
        (*BufferPtr).Q = 0; // clear the value
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:24:03+00:00Added an answer on May 24, 2026 at 8:24 am

    You could create a structure for a buffer with a zero/one length array for Q and then union it with the real structure for the buffer you want to use. You’d then also need to have some way of knowing the length that you are meant to use, which you could either get from your mask type or an additional structure member.

    struct BuffType
    {
        int Head;
        int Tail;
        int Mask;
        char Full;
        char Empty;
        int Q[1];
    };
    
    struct BuffType1_storage
    {
        int Head;
        int Tail;
        int Mask;
        char Full;
        char Empty;
        int Q[BUFF_TYPE_1_SIZE];
    };
    
    union BuffType1
    {
        struct BuffType b;
        struct BuffType1_storage b1;
    }
    
    union BuffType1 MyBuff1[4];
    

    You can then pass a pointer to the union BuffType1 or struct BuffType b member and you know that the memory behind it is there for a particular type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Cocoa application with Mono embedded. I want to run and
I am writing an application to run on a windows embedded ce 6.0. the
I'm about to write an application for a embedded device with an nvidia Tagra2
I'm writing some C code for an embedded application, and I've run into a
I'm writing a C# application which has IronPython (2.0.1) embedded in it. The idea
I am writing the firmware for an embedded device in C using the Silicon
I'm writing a program that has an NSView embedded in an NSScrollView which user
I am writing a C++ application for an embedded Linux. I have a problem
Writing a small application in order to demonstrate certain functionality. I have an embedded
I am writing a web application that will run in kiosk mode on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.