“typedef void (*Something)()”是什么意思?意思是
我试图理解这意味着什么,我正在查看的代码
位于 .h
中
typedef void (*MCB)();
static MCB m_process;
的 .C
MCB Modes::m_process = NULL;
有时,当我这样做时
m_process();
,我会遇到分段错误,这可能是因为内存被释放,释放后如何调试?
I am trying to understand what this means, the code I am looking at has
in .h
typedef void (*MCB)();
static MCB m_process;
in .C
MCB Modes::m_process = NULL;
And sometimes when I do
m_process();
I get segmentations fault, it's probably because the memory was freed, how can I debug when it gets freed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它定义了一个函数指针类型。函数返回 void,并且未指定参数列表,因为问题(当前但可能错误地)标记为 C;如果它被标记为 C++,那么该函数将根本不带任何参数。要使其成为不带参数的函数(在 C 中),您可以使用:
这是 C 之间存在显着差异的领域之一,C 还不需要在定义之前对所有函数进行原型设计或使用过,还有 C++,确实如此。
It defines a pointer-to-function type. The functions return void, and the argument list is unspecified because the question is (currently, but possibly erroneously) tagged C; if it were tagged C++, then the function would take no arguments at all. To make it a function that takes no arguments (in C), you'd use:
This is one of the areas where there is a significant difference between C, which does not - yet - require all functions to be prototyped before being defined or used, and C++, which does.
让我们举个例子
,在这里,我们定义了一个类型 pt2fn。这种类型的变量指向函数,该函数采用整数作为参数并且不返回任何值。
这里,kk 是一个 pt2fn 类型的变量,它可以指向任何以整数作为输入并且不返回任何值的函数。
参考:https://cs.nyu.edu /courses/spring12/CSCI-GA.3033-014/Assignment1/function_pointers.html
Let's take an example
Here, we are defining a type pt2fn. Variables of this type point to functions, that take an integer as argument and does not return any value.
Here, kk is a variable of type pt2fn, which can point to any function that takes in an integer as input and does not return any value.
Reference:https://cs.nyu.edu/courses/spring12/CSCI-GA.3033-014/Assignment1/function_pointers.html
它引入了函数指针类型,指向不返回任何内容(void)、不带任何参数的函数,并将新类型命名为MCB。
It introduces a function pointer type, pointing to a function returning nothing (void), not taking any parameters and naming the new type MCB.
typedef 将
MCB
定义为指向不带参数并返回void
的函数的指针类型。请注意,
MCB Modes::m_process = NULL;
是 C++,而不是 C。此外,在 C 中,typedef 实际上应该是typedef void (*MCB)(void);
。我不确定你所说的“内存已释放”是什么意思。你有一个指向函数的静态指针;函数无法被释放。最多,你的指针已经在某个地方重置了。只需使用
m_process
上的内存监视进行调试即可。The typedef defines
MCB
as the type of a pointer to a function that takes no arguments, and returnsvoid
.Note that
MCB Modes::m_process = NULL;
is C++, not C. Also, in C, the typedef should really betypedef void (*MCB)(void);
.I'm not sure what you mean by "the memory was freed". You have a static pointer to a function; a function cannot be freed. At most, your pointer has been reset somewhere. Just debug with a memory watch on
m_process
.这是一个函数指针。您会收到 SEGMENTATION FAULT,因为您尝试调用地址无效 (
NULL
) 的函数。根据您的具体示例,该函数不应返回任何值 (
void
),并且不应接收任何参数()
。这应该可行:
函数指针通常用于 C 中某种形式的事件处理。但这并不是它的唯一用途......
It's a function pointer. You get a SEGMENTATION FAULT because you are trying to make a call to a function which address is invalid (
NULL
).According to your specific sample, the function should return no value (
void
) and should receive no parameters()
.This should work:
Function pointers are commonly used for some form of event handling in C. It's not its only use though...