没有malloc的多队列大小 [英] Multi size queue without malloc

查看:215
本文介绍了没有malloc的多队列大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感兴趣的构建,可以得到不同的定义,但尺寸的队列。比方说,有8个,16和32个元素,我想这样做,而不使用malloc。
好吧,如果我创建3个不同的队列,但我不希望做到这一点很容易,我想用同样的功能,只是定义了三种类型。

I am interested in build a queue that could receive different but defined sizes. Let's say, with 8, 16 and 32 elements and I want to do it without use malloc. Well, it would be easy if I create 3 different queues but I don't want to do this, I want to use the same functions and just define the three types.

我的问题是正是这一点,我有我的code三个地方,我想用一个队列,但这种情况下会使用队列,不同大小和我知道它是什么规模。我不想创建三个组功能和结构,使这个队列中,我想刚才创建三个结构,并使用相同的功能。另外,我不能使用malloc,因为它是一个嵌入式应用。

My problem is exactly this, I have three places in my code that I would like to use a queue, but those cases would use queues with different sizes and I know what size it is. I don't want to create three group of functions and structs to make this queue, I would like to just create three structs and use the same functions. Also, I can't use malloc, because it is a embedded application.

我想收到的是如何创造的东西作为抽象类型的C ++在C.它可以解决我的问题的想法。

I want to receive ideas of how to create something as abstract type of C++ in C. It could solve my problem.

有没有什么办法这样做呢?

Is there any way to to this?

谢谢!

推荐答案

有几种方法可以做到这一点。在这里,一对夫妇的...

There are several ways to do this. Here a couple...


  1. 包含在队列首标队列的大小;访问队列中的函数使用大小从标题限制条目的数量

  1. Include the size of the queue in the queue header; the functions that access the queue use the size from the header to limit the number of entries

传递队列的大小作为参数传递给该管理队列功能

Pass the queue size as an argument to the functions that manage the queue

第一个选项是不易出错,但需要一个额外的头字段。

The first option is less error prone, but takes an extra header field.

的结构将被定义为

static struct s_que8
{
   int in;
   int out;
   int size; // for option 1
   int elements[8]; // or whatever size you like
} que8;

在任何情况下,队列将要求和退出指数的初始化,而<$ C $对于选项1. C>尺寸值如果您的队列协议是在==出表示队列是空的,你可以设置在=出= 0; 来初始化它们

In any case the queues will require initialization of the in and out indices, and the size value for option 1. If your queue protocol is that in == out means the queue is empty, you can just set in = out = 0; to initialize them.

附录

对于那些静态字符串,这里有一些具体的结构声明元素:

For elements that are static strings, here are some specific structure declarations:

static struct s_que8
{
   int in;
   int out;
   int size; // for option 1
   char * elements[8]; // for pointers to static strings
} que8;

static struct s_que8
{
   int in;
   int out;
   int size; // for option 1
   char elements[8][MAX_STRING_SIZE]; // for strings stored in the queue
} que8;

修改 8 来满足您的特定队列的大小。

Change 8 to suit your specific queue size.

相应的通用的类型定义应该是:

The corresponding "generic" typedefs would be:

typedef struct s_queX
{
   int in;
   int out;
   int size; // for option 1
   char * elements[]; // for pointers to static strings
} Queue;

typedef struct s_queX
{
   int in;
   int out;
   int size; // for option 1
   char elements[][MAX_STRING_SIZE]; // for strings stored in the queue
} Queue;

这篇关于没有malloc的多队列大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆