gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:34:48 26 4
gpt4 key购买 nike

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

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

  • 4x 1024+ int(+ 意味着如果可用的话,需要更多)
  • 4x 256 整数
  • 1x 256 int(与上述应用程序不同)
  • 1x 2048+ int(+ 意味着如果可用,则需要更多)

我已经在下面的代码中实现了它。您会注意到我创建了 2 个缓冲区作为大小为 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
}

最佳答案

您可以为 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 BuffType1 或结构 BuffType b 成员,并且您知道它背后的内存是针对特定类型的。

关于c - 如何将多个结构传递给一个函数 (ANSI C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6959349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com