- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我第 64 次尝试编写缓冲库,并且我开始涉足一些非常高级的东西。我想就此征求一些专业意见。
在我的第一个头文件中我有这个:
typedef struct StdBuffer { void* address; } StdBuffer;
extern void StdBufferClear(StdBuffer);
在 #includes
第一个头文件的另一个头文件中,我有这个:
typedef struct CharBuffer { char* address; } CharBuffer;
void (*CharBufferClear)(CharBuffer) = (void*) StdBufferClear;
将此函数指针声明为void 会干扰调用吗?它们具有按值签名匹配。我以前从未见过声明为 void 的函数指针,但这是让它干净地编译的唯一方法。
从堆栈上看,它与我在汇编代码中学到的东西应该没有任何区别。
无关紧要天哪!我刚刚在 StackOverflow 上说过 Stackwise!
嗯.. 看来我在这里假设太多了。如果可以,请允许我再次澄清。我不在乎地址中存储的是什么“类型”的数据。我所关心的只是一个“单元”的大小以及该地址有多少个单元。如果您愿意,请查看 API 的接口(interface)协议(protocol)契约(Contract):
typedef struct StdBuffer {
size_t width; ///< The number of bytes that complete a data unit.
size_t limit; ///< The maximum number of data units that can be allocated for this buffer.
void * address; ///< The memory address for this buffer.
size_t index; ///< The current unit position indicator.
size_t allocated; ///< The current number of allocated addressable units.
StdBufferFlags flags;///< The API contract for this buffer.
} StdBuffer;
你看,memcpy、memmove 和类似的东西并不真正关心地址中的内容,他们想要的只是我在这里清楚地跟踪的细节。
现在看看第一个遵循这个契约的原型(prototype):
typedef struct CharBuffer {
size_t width; ///< The number of bytes that complete a data unit.
size_t limit; ///< The maximum number of data units that can be allocated for this buffer.
char * address; ///< The memory address for this buffer.
size_t index; ///< The current unit position indicator.
size_t allocated; ///< The current number of allocated addressable units.
CharBufferFlags flags;///< The API contract for this buffer.
} CharBuffer;
如您所见,数据类型在此上下文中无关紧要。你可以说 C 会根据不同的情况对其进行不同的处理,但归根结底,一个 address
就是一个 address
,一个 byte
是 byte
而 long
是 long
因为我们在同一台机器上处理内存。
这个系统组合在一起的目的是消除所有这种基于类型的杂耍,C 似乎为此感到自豪(而且理所当然......)它对我想做的事情毫无意义。这是为位于任何地址的任何标准大小的数据(1、2、4、8、sizeof(RandomStruct))创建一个遵守契约(Contract)的原型(prototype)。
能够使用代码执行我自己的转换并使用 api 函数操作该数据,这些函数在具有特定长度内存单元的特定长度内存块上运行。然而,原型(prototype)必须包含正式的数据指针类型,因为对于最终用户来说,每次他们想使用该地址指针做某事时都必须重新转换他们的数据是没有意义的。如果指针为空,将其称为 CharBuffer 是没有意义的。
StdBuffer
是一种通用类型,除了在 api 本身之外从未使用过,用于管理所有遵守契约(Contract)的数据类型。
该系统将包含的 API 来 self 最新版本的缓冲。这里有很清楚的记录 @Google Code我知道有些事情需要改变才能将所有这些整合在一起,即如果没有大量适当的研究和意见收集,我将无法直接从 API 中安全地操作数据。
这引起了我的注意,我还需要 StdBufferFlags 成员中的有符号/无符号位标志。
也许这个拼图的最后一 block 也是为了让您仔细阅读。
/** \def BIT(I)
\brief A macro for setting a single constant bit.
*
* This macro sets the bit indicated by I to enabled.
* \param I the (1-based) index of the desired bit to set.
*/
#define BIT(I) (1UL << (I - 1))
/** \enum StdBufferFlags
\brief Flags that may be applied to all StdBuffer structures.
* These flags determine the contract of operations between the caller
* and the StdBuffer API for working with data. Bits 1-4 are for the
* API control functions. All other bits are undefined/don't care bits.
*
* If your application would like to use the don't care bits, it would
* be smart not to use bits 5-8, as these may become used by the API
* in future revisions of the software.
*/
typedef enum StdBufferFlags {
BUFFER_MALLOCD = BIT(1), ///< The memory address specified by this buffer was allocated by an API
BUFFER_WRITEABLE = BIT(2), ///< Permission to modify buffer contents using the API
BUFFER_READABLE = BIT(3), ///< Permission to retrieve buffer contents using the API
BUFFER_MOVABLE = BIT(4) ///< Permission to resize or otherwise relocate buffer contents using the API
}StdBufferFlags;
最佳答案
此代码需要诊断:
void (*CharBufferClear)(CharBuffer) = (void*) StdBufferClear;
您正在将一个 void *
指针转换为一个没有转换的函数指针。在 C 中,void *
指针可以转换为指向对象类型的指针而无需强制转换,但不能转换为函数指针类型。 (在 C++ 中,为了增加安全性,还需要强制转换将 void *
转换为对象类型。)
你在这里想要的只是在函数指针类型之间进行转换,即:
void (*CharBufferClear)(CharBuffer) = (void (*)(CharBuffer)) StdBufferClear;
那么你仍然在做相同的类型双关,因为函数是不同的类型。您正在尝试使用指向采用 CharBuffer
的函数的指针来调用采用 StdBuffer
的函数。
这种类型的代码不是定义良好的 C。打败了类型系统后,您只能靠自己,依靠测试、检查目标代码,或者从编译器编写者那里获得一些此类事情有效的保证使用那个编译器。
您在汇编代码中学到的知识并不适用,因为汇编语言只有少量基本数据类型,例如“机器地址”或“32 位字”。具有相同布局和低级表示的两个数据结构可能是不兼容类型的概念不会出现在汇编语言中。
即使两种类型在底层看起来相同(另一个例子:unsigned int
和 unsigned long
有时完全相同)C 编译器可以基于没有违反类型规则的假设。例如,假设 A
和 B
指向相同的内存位置。如果您分配给对象 A->member
,C 编译器可以假定对象 B->member
不受此影响,如果 A->member
和 B->member
有不兼容的类型,比如一个是 char *
而另一个是 void *
。生成的代码一直在寄存器中缓存 B->member
的旧值,即使内存中的副本已被分配给 A->member
覆盖。这是无效别名的示例。
关于c - 将函数指针转换为 void 有什么影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10305192/
这是一个非常笼统的问题,我希望我能答对。 我正在研究 SSL/TLS 重新协商并已阅读了一些内容。这是我从阅读中了解到的内容: 从 SSL/TLS 重新协商的角度来看,客户端分为两个主要组,打补丁的和
第一个屏幕是艺术的细节。当我向上滚动时,标题将是 alpha。我点击另一个“艺术”到另一个细节 UI,然后按回到 Previous UI。之前的UI标题是黑色的,怎么变透明了。 布局:
想知道 mv 对基表的影响。它会减慢基表的速度吗?它什么时候开始写入 mv,就像同时写入基表和 mv 一样? 如果我有 local_quorum 的 CL 且 RF=3,客户端是否必须等到写入 mv
似乎在任何地方都找不到太多关于此问题的帮助,所以我想我会在这里尝试。 我正在尝试制作一个简单的 for 循环,当我将鼠标悬停在 html 卡上时,它会隐藏卡中的一些文本。该卡有一个简单的名字和姓氏,我
我有一个程序每帧运行 tick() 方法。我希望一个对象根据设定的重力常数下落,因此我创建了一个 Ball 对象,该对象会将其位置更新为前一帧的位置减去 y 速度。每个刻度 y 速度都会减少重力常数。
我的 KeyHandler 在这里: private void KeyHandler(java.awt.event.KeyEvent evt) {
我有一个方法,其中使用了很多其他类,包括链接列表、队列和堆栈。在我的方法中,我有一个 for 循环,我想在其中弹出堆栈(方便地命名为 s)并将队列(方便地命名为 q)出队到 s1 和 q1。由于某种原
我有一个 JTree 节点数组和另一个自定义对象的相应数组。 我想要什么:当选择 JTree 的节点时,相应对象(其数组中索引与节点数组中所选节点索引相同的对象)的字段填充 JLabels。 我被困在
我知道浏览器完成了处理客户端脚本(Javascript、JQuery 等)的所有工作,但想知道在性能方面是否还有其他重要因素(网络速度、客户端计算机速度、服务器环境) 如果它完全依赖于浏览器(类型和版
我有一个 Android 服务在后台运行,它将使用以下代码: while(true) { ServerSocket server = new ServerSocket(1234); Socke
对JQM有以下疑惑: 1.如果我们在单独的 html 文件中使用重复的 id,对 jquery mobile 有什么影响。 假设我们在单独的 html 文件中有重复的 id,但如果我们不使用该 id
我正在尝试更新两个(inventory、sold)MySQL 表的表库存。 假设我们正在处理的 sku 是 BT888-16 UPDATE inventory JOIN sold ON invento
我使用这种方法来更改我的表格单元格值, 它在 jtable 上改变但在文本文件上没有改变! public class user_AllBooks extends AbstractTableModel
我想在向表中插入数据时创建一个 MYSQL 存储过程,数据也会被插入到其他服务器表中。 我知道这在 ORACLE 数据库中是可能的,但我不知道它是否适用于 MYSQL。 有什么办法吗? 最佳答案 是的
我在 css 方面非常糟糕,只能靠 SO 答案来解决 - 但是我找不到针对这个特定问题的任何解释。 我有一个表单,其中包含一个 textarea 和一个 button(input/submit),仅此
我在一个元素上有动画,但它的移动也会影响 sibling 。如何在不影响兄弟元素的情况下仅在元素上使用动画? 问题示例: function animateSearch() { $('.glyph
我试图在我的 ViewController 中的 UIView 的所有四个边上建立一个阴影 — 在我通过 Xcode 向 UIView 添加约束之前,它工作得很好。我怎样才能使 UIView 的阴影显
自从我使用 JavaScript 以来已经有一段时间了 - 在获得证书之后我开始学习 Perl 并从那时起就一直使用它。我只是想重新开始使用 JS,我已经写了这个,我想说的是,这是一个简单的小脚本,可
我正在处理一个 HTML 元素,我添加了一个复选框,选中后会高亮显示所有文本输入字段。唯一的问题是一些输入字段在表格内,出于某种原因我无法用我的代码影响它们。任何帮助将不胜感激。 相关代码: HTML
我为 String 类创建了一个小扩展,以便方便地从中删除字符。这是它的样子: mutating func drop(characters chars: [String]) { for c i
我是一名优秀的程序员,十分优秀!