Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将多个结构传递给一个函数(ANSI C)_C_Arrays_Struct_Buffer_Typedef - Fatal编程技术网

如何将多个结构传递给一个函数(ANSI C)

如何将多个结构传递给一个函数(ANSI C),c,arrays,struct,buffer,typedef,C,Arrays,Struct,Buffer,Typedef,我正在编写一个嵌入式应用程序,其运行频率约为3-7 MHz,因此速度至关重要。此外,该设备只有32K的RAM动态内存分配不是可选的。也就是说 我正在编写一个缓冲程序,它需要具有不同队列长度的循环缓冲区 4x 1024+int(+如果可用,意味着需要更多) 4x 256整数 1x 256 int(与上述应用程序不同) 1x 2048+整数(+如果可用,则表示需要更多) 我已经在下面的代码中实现了它。您会注意到,我创建了两个缓冲区作为大小为1的数组,这是因为我希望使用相同的ReadBuff和Wr

我正在编写一个嵌入式应用程序,其运行频率约为3-7 MHz,因此速度至关重要。此外,该设备只有32K的RAM动态内存分配不是可选的。也就是说

我正在编写一个缓冲程序,它需要具有不同队列长度的循环缓冲区

  • 4x 1024+int(+如果可用,意味着需要更多)
  • 4x 256整数
  • 1x 256 int(与上述应用程序不同)
  • 1x 2048+整数(+如果可用,则表示需要更多)
我已经在下面的代码中实现了它。您会注意到,我创建了两个缓冲区作为大小为1的数组,这是因为我希望使用相同的ReadBuff和WriteBuff函数访问所有缓冲区。我的问题是,当您将结构指针传递给函数时,编译器希望指针数据类型相同。但是,将指向BuffType2数据类型的指针传递给需要BuffType1的函数在C中有点非法,即使结构的结构与队列大小完全相同,也可能无法正常工作。解决此问题的唯一方法是在创建所需的各种结构数组后动态分配队列的大小

您还应该知道,我已经将缓冲区设计为从尾部读取并写入头部,它的设计使得当头部和尾部索引通过缓冲区大小时,它会溢出到0

下面是一些代码

/***************************************************************\
    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
    }

暂且不考虑性能因素、内存大小影响以及多结构类型输入参数,您是否考虑过创建一个header结构来描述每个单独的缓冲区,并使用一个指向该缓冲区的指针?通过这种方式,您将传递一个单一的结构类型(缓冲区元数据),然后让处理函数自行决定如何继续

编辑-- 作为一个例子,您可以采用BuffTypeX类型,并将缓冲区信息压缩为元结构,如下所示:

typedef struct buffMetaData
  {
  int Head, Tail, Mask, Full, Empty...
  int *PtrToBuff
  };
您的缓冲区将在适当的位置定义为直数组,在初始化过程中,您必须初始化buffMetaData对象并将它们指向适当的存储位置

然后实现处理函数,接收buffMetaData类型,读取内容,并根据需要处理每个缓冲区(可能基于掩码或buffMetaData字段可能包含的任何唯一ID标签)


诚然,这种方法可能会占用大量内存,因为所有缓冲区都需要在分配之前全部分配完毕,而且还需要为堆栈、堆和其他变量保存RAM。

您可以为缓冲区创建一个结构,为Q创建一个零/一长度数组,然后将其与要使用的缓冲区的实际结构合并。然后,您还需要知道要使用的长度,可以从掩码类型或其他结构成员中获得

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];

然后,您可以传递一个指向union BuffType 1或struct BuffType b成员的指针,并且您知道它背后的内存是用于特定类型的。

我非常喜欢这个想法,但我有一个问题。当使用一个数据成员时,例如BuffType1.BuffType1_storage.Head,我是否需要担心遵守联合所需的额外时间,或者它是否需要与BuffType1_storage.Head相同数量的指令(如果在这种情况下没有联合的话)很酷,我不经常使用联合,所以我从未想过这一点。问:对于联合体,内部组件(ByffType和BuffType1_存储)的总体内存占用是否需要相同,或者分配是否会向较大的内存占用方向错误,并将较小的内存占用推入内部?@Jeremy:使用BuffType1.Head和联合体BuffType1.BuffType1_存储.Head之间应该没有任何区别。它仍然只是在与变量的某个偏移量处从内存访问变量。联合在运行时不会创建任何额外的解引用,它只是不同类型叠加在同一内存上。@spade78:编译器将使你的联合至少与其中包含的最大成员一样大。@spade78:这不重要,您可以在2048为这两个变量创建一个typedef,但当您将其分配到union下的union变量名中时,这两个变量的总数仍然是2048,因为它们只是按位叠加