C 中的关联数组 [英] Associative arrays in C

查看:27
本文介绍了C 中的关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施一种将一组数据传输到可编程加密狗的方法.加密狗基于智能卡技术,可以在内部执行任意代码.输入和输出数据作为二进制块传递,可以通过输入和输出指针访问.

I am implementing a way to transfer a set of data to a programmable dongle. The dongle is based on a smart card technology and can execute an arbitrary code inside. The input and output data is passed as a binary blocks that can be accessed via input and output pointers.

我想使用关联数组来简化数据处理代码.一切都应该这样工作:

I would like to use an associative array to simplify the data processing code. Everything should work this way:

首先是宿主应用程序:

// Host application in C++
in_data["method"] = "calc_r";
in_data["id"] = 12;
in_data["loc_a"] = 56.19;
in_data["loc_l"] = 44.02;
processor->send(in_data);

接下来是加密狗内的代码:

Next the code inside the dongle:

// Some dongle function in C
char* method_name = assoc_get_string(in_data, "method");
int id = assoc_get_int(in_data, "id");
float loc_a = assoc_get_float(in_data, "loc_a");
float loc_l = assoc_get_float(in_data, "loc_l");

所以我的问题是关于加密狗部件的功能.有没有 C 代码或库来实现像上面这样的关联数组行为?

So my question is about the dongle part functionality. Is there C code or library to implement such an associative array behavior like the above?

推荐答案

我怀疑您必须自己编写.如果我理解您所描述的架构,那么您将需要将整个数据块发送到一个单独的部分.如果是这样,那么大多数库都不会为此工作,因为它们很可能会分配多块内存,这将需要多次传输(以及对结构的内部理解).这类似于尝试使用库哈希函数,然后仅通过将根指针传递给 send 函数,通过套接字在网络上发送其内容.

My suspicion is that you would have to write your own. If I understand the architecture you are describing, then you will need to send the entire chunk of data in a single piece. If so, then most libraries will not work for that because they will most likely be allocating multiple pieces of memory, which would require multiple transfers (and an inside understanding of the structure). It would be similar to trying to use a library hash function and then sending its contents over the network on a socket just by passing the root pointer to the send function.

您可以编写一些自己的实用程序,在单个内存块中管理一个非常简单的关联数组(或散列).如果数据量很小,它可以对条目使用简单的线性搜索,这将是一段相当紧凑的代码.

It would be possible to write some utilities of your own that manage a very simple associative array (or hash) in a single block of memory. If the amount of data is small, it could use a simple linear search for the entries and would be a fairly compact bit of code.

这篇关于C 中的关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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