- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用结构创建文本解析状态机。它似乎适用于我用来测试它的较小结构,但我的最终应用程序(调制解调器控制)所需的较大支柱似乎没有像我希望的那样在内存中表示。
我认为问题在于初始化这个结构:
struct state
{
size_t const id;
size_t(* const callback)(void*);
void* const callback_arg;
char const* const out_pattern;
size_t const out_pattern_length;
unsigned long int timeout;
size_t const timeout_state;
size_t const next_states_length;
struct next_state const* next_states;
};
使用这个宏:
#define sm_state(id, callback, callback_arg, out_str, out_str_length, timeout, \
timeout_state, next_states_length, ...) \
{id, callback, callback_arg, out_str, out_str_length, timeout, \
timeout_state, next_states_length, \
(struct next_state const[next_states_length]){__VA_ARGS__}}
next_state 在哪里:
#define sm_next_state(next_state_id, in_pattern_length, in_pattern) \
{next_state_id, in_pattern_length, in_pattern}
struct next_state
{
size_t const next_state;
size_t const in_pattern_length;
char const* in_pattern;
};
有效的测试用例是:
enum states_enum
{
fruit = 1,
vegitable,
error,
};
struct state const states[] = {
// id callback callback argument pattern to be sent on entry patlen timeout t/o state next states count next_states
sm_state( fruit, NULL, NULL, "hello, pick a fruit\n", 20, 100000UL, 1, 3, sm_next_state(fruit, 4, "pear"), sm_next_state(vegitable, 5, "apple"), sm_next_state(error, 6, "turnip")),
sm_state( vegitable, NULL, NULL, "now pick a vegetable\n", 21, 100000UL, 3, 3, sm_next_state(fruit, 7, "cabbage"), sm_next_state(vegitable, 6, "potato"), sm_next_state(error, 6, "turnip")),
sm_state( error, NULL, NULL, "error, you entered turnip...\n", 29, 100000UL, 1, 3, sm_next_state(fruit, 3, "one"), sm_next_state(vegitable, 3, "two"), sm_next_state(error, 6, "turnip"))
};
int i = 0;
while (i < 3)
{
printf("id: %d\n", states[i].id);
++i;
}
输出:
id: 1
id: 2
id: 3
还有更大的结构不起作用:
enum bluetooth_states_enum
{
bt_off = 1,
bt_on,
bt_set_echo_off,
bt_set_auto_answer,
bt_set_ignore_dsr,
bt_set_spp,
bt_set_io_cap,
bt_set_mitm_protection,
bt_write,
bt_reset,
bt_set_firendly_name,
bt_set_connectable,
bt_set_key,
bt_listening,
bt_answer,
bt_in_call,
bt_hup,
bt_error,
};
struct state const states[] = {
sm_state( bt_off, NULL, NULL, "", 0, -1, bt_off, 0,),
sm_state( bt_on, NULL, NULL, "AT\r", 3, 100000UL, bt_error, 2, sm_next_state(bt_set_echo_off, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_echo_off, NULL, NULL, "ATE0\r", 5, 100000UL, bt_error, 2, sm_next_state(bt_set_auto_answer, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_auto_answer, NULL, NULL, "ATS0=2\r", 7, 100000UL, bt_error, 2, sm_next_state(bt_set_ignore_dsr, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_ignore_dsr, NULL, NULL, "ATS102=1\r", 9, 100000UL, bt_error, 2, sm_next_state(bt_set_spp, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_spp, NULL, NULL, "ATS102=1\r", 9, 100000UL, bt_error, 2, sm_next_state(bt_set_io_cap, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_io_cap, NULL, NULL, "ATS321=0\r", 9, 100000UL, bt_error, 2, sm_next_state(bt_set_mitm_protection, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_mitm_protection, NULL, NULL, "ATS322=1\r", 9, 100000UL, bt_error, 2, sm_next_state(bt_write, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_write, NULL, NULL, "AT&W\r", 5, 100000UL, bt_error, 2, sm_next_state(bt_reset, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_reset, NULL, NULL, "ATZ\r", 4, 100000UL, bt_error, 2, sm_next_state(bt_set_firendly_name, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_firendly_name, NULL, NULL, "AT+BTF=\"Friendly Name\"\r", 18, 100000UL, bt_error, 2, sm_next_state(bt_set_connectable, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_connectable, NULL, NULL, "AT+BTP\r", 7, 100000UL, bt_error, 2, sm_next_state(bt_set_key, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_key, NULL, NULL, "AT+BTK=\"8475\"\r", 14, 100000UL, bt_error, 2, sm_next_state(bt_listening, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_listening, NULL, NULL, "", 0, -1, 0, 1, sm_next_state(bt_answer, 4, "RING")),
sm_state( bt_answer, NULL, NULL, "ATA\r", 4, 100000UL, bt_error, 2, sm_next_state(bt_in_call, 7, "CONNECT"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_in_call, NULL, NULL, "", 0, 100000UL, bt_error, 2, sm_next_state(bt_set_connectable, 10, "NO CONNECT"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_error, NULL, NULL, "", 0, -1, bt_off, 1, sm_next_state(bt_off, 0, ""))
};
输出:
id: 1820602156
id: 7
id: -1
id: 1973037073
id: 0
id: 0
id: 123336558
id: 0
id: 0
id: 0
id: 736
id: 760826203
id: 3108
id: -1322777276
id: 1
id: 7916
id: 152
似乎内存中不存在更大的示例。如果有人能指出我做错了什么,那就太棒了。
最佳答案
所以看起来问题在于您没有在声明 states[0]
中向 sm_state
传递足够的参数。
如果你这样声明它会起作用:
struct state const states[] = {
{bt_off, NULL, NULL, "", 0, -1, bt_off, 0},
sm_state( bt_on, NULL, NULL, "AT\r", 3, 100000UL, bt_error, 2, sm_next_state(bt_set_echo_off, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_echo_off, NULL, NULL, "ATE0\r", 5, 100000UL, bt_error, 2, sm_next_state(bt_set_auto_answer, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_auto_answer, NULL, NULL, "ATS0=2\r", 7, 100000UL, bt_error, 2, sm_next_state(bt_set_ignore_dsr, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_ignore_dsr, NULL, NULL, "ATS102=1\r", 9, 100000UL, bt_error, 2, sm_next_state(bt_set_spp, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_spp, NULL, NULL, "ATS102=1\r", 9, 100000UL, bt_error, 2, sm_next_state(bt_set_io_cap, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_io_cap, NULL, NULL, "ATS321=0\r", 9, 100000UL, bt_error, 2, sm_next_state(bt_set_mitm_protection, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_mitm_protection, NULL, NULL, "ATS322=1\r", 9, 100000UL, bt_error, 2, sm_next_state(bt_write, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_write, NULL, NULL, "AT&W\r", 5, 100000UL, bt_error, 2, sm_next_state(bt_reset, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_reset, NULL, NULL, "ATZ\r", 4, 100000UL, bt_error, 2, sm_next_state(bt_set_firendly_name, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_firendly_name, NULL, NULL, "AT+BTF=\"Friendly Name\"\r", 18, 100000UL, bt_error, 2, sm_next_state(bt_set_connectable, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_connectable, NULL, NULL, "AT+BTP\r", 7, 100000UL, bt_error, 2, sm_next_state(bt_set_key, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_set_key, NULL, NULL, "AT+BTK=\"8475\"\r", 14, 100000UL, bt_error, 2, sm_next_state(bt_listening, 2, "OK"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_listening, NULL, NULL, "", 0, -1, 0, 1, sm_next_state(bt_answer, 4, "RING")),
sm_state( bt_answer, NULL, NULL, "ATA\r", 4, 100000UL, bt_error, 2, sm_next_state(bt_in_call, 7, "CONNECT"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_in_call, NULL, NULL, "", 0, 100000UL, bt_error, 2, sm_next_state(bt_set_connectable, 10, "NO CONNECT"), sm_next_state(bt_error, 5, "ERROR")),
sm_state( bt_error, NULL, NULL, "", 0, -1, bt_off, 1, sm_next_state(bt_off, 0, ""))
};
当我们讨论这个主题时,我强烈建议您删除这些宏。当无法破译编译器错误消息时,它们只提供 stackoverflow 问题。我建议像使用 states[0]
一样初始化每个变量。
关于c - 使用嵌套结构数组进行结构初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26843114/
我目前正在尝试基于哈希表构建字典。逻辑是:有一个名为 HashTable 的结构,其中包含以下内容: HashFunc HashFunc; PrintFunc PrintEntry; CompareF
如果我有一个指向结构/对象的指针,并且该结构/对象包含另外两个指向其他对象的指针,并且我想删除“包含这两个指针的对象而不破坏它所持有的指针”——我该怎么做这样做吗? 指向对象 A 的指针(包含指向对象
像这样的代码 package main import "fmt" type Hello struct { ID int Raw string } type World []*Hell
我有一个采用以下格式的 CSV: Module, Topic, Sub-topic 它需要能够导入到具有以下格式的 MySQL 数据库中: CREATE TABLE `modules` ( `id
通常我使用类似的东西 copy((uint8_t*)&POD, (uint8_t*)(&POD + 1 ), back_inserter(rawData)); copy((uint8_t*)&PODV
错误 : 联合只能在具有兼容列类型的表上执行。 结构(层:字符串,skyward_number:字符串,skyward_points:字符串)<> 结构(skyward_number:字符串,层:字符
我有一个指向结构的指针数组,我正在尝试使用它们进行 while 循环。我对如何准确初始化它并不完全有信心,但我一直这样做: Entry *newEntry = malloc(sizeof(Entry)
我正在学习 C,我的问题可能很愚蠢,但我很困惑。在这样的函数中: int afunction(somevariables) { if (someconditions)
我现在正在做一项编程作业,我并没有真正完全掌握链接,因为我们还没有涉及它。但是我觉得我需要它来做我想做的事情,因为数组还不够 我创建了一个结构,如下 struct node { float coef;
给定以下代码片段: #include #include #define MAX_SIZE 15 typedef struct{ int touchdowns; int intercepti
struct contact list[3]; int checknullarray() { for(int x=0;x<10;x++) { if(strlen(con
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Empty “for” loop in Facebook ajax what does AJAX call
我刚刚在反射器中浏览了一个文件,并在结构构造函数中看到了这个: this = new Binder.SyntaxNodeOrToken(); 我以前从未见过该术语。有人能解释一下这个赋值在 C# 中的
我经常使用字符串常量,例如: DICT_KEY1 = 'DICT_KEY1' DICT_KEY2 = 'DICT_KEY2' ... 很多时候我不介意实际的文字是什么,只要它们是独一无二的并且对人类读
我是 C 的新手,我不明白为什么下面的代码不起作用: typedef struct{ uint8_t a; uint8_t* b; } test_struct; test_struct
您能否制作一个行为类似于内置类之一的结构,您可以在其中直接分配值而无需调用属性? 前任: RoundedDouble count; count = 5; 而不是使用 RoundedDouble cou
这是我的代码: #include typedef struct { const char *description; float value; int age; } swag
在创建嵌套列表时,我认为 R 具有对列表元素有用的命名结构。我有一个列表列表,并希望应用包含在任何列表中的每个向量的函数。 lapply这样做但随后剥离了列表的命名结构。我该怎么办 lapply嵌套列
我正在做一个用于学习目的的个人组织者,我从来没有使用过 XML,所以我不确定我的解决方案是否是最好的。这是我附带的 XML 文件的基本结构:
我是新来的 nosql概念,所以当我开始学习时 PouchDB ,我找到了这个转换表。我的困惑是,如何PouchDB如果可以说我有多个表,是否意味着我需要创建多个数据库?因为根据我在 pouchdb
我是一名优秀的程序员,十分优秀!