“typedef void (*Something)()”是什么意思?意思是

发布于 2024-09-28 08:45:55 字数 301 浏览 2 评论 0原文

我试图理解这意味着什么,我正在查看的代码

位于 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迷雾森÷林ヴ 2024-10-05 08:45:55

它定义了一个函数指针类型。函数返回 void,并且未指定参数列表,因为问题(当前但可能错误地)标记为 C;如果它被标记为 C++,那么该函数将根本不带任何参数。要使其成为不带参数的函数(在 C 中),您可以使用:

typedef void (*MCB)(void);

这是 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:

typedef void (*MCB)(void);

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.

云裳 2024-10-05 08:45:55

让我们举个例子

typedef void (*pt2fn)(int);

,在这里,我们定义了一个类型 pt2fn。这种类型的变量指向函数,该函数采用整数作为参数并且不返回任何值。

pt2fn kk;

这里,kk 是一个 pt2fn 类型的变量,它可以指向任何以整数作为输入并且不返回任何值的函数。

参考:https://cs.nyu.edu /courses/spring12/CSCI-GA.3033-014/Assignment1/function_pointers.html

Let's take an example

typedef void (*pt2fn)(int);

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.

pt2fn kk;

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

ゝ偶尔ゞ 2024-10-05 08:45:55

它引入了函数指针类型,指向不返回任何内容(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.

策马西风 2024-10-05 08:45:55

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 returns void.

Note that MCB Modes::m_process = NULL; is C++, not C. Also, in C, the typedef should really be typedef 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.

雪化雨蝶 2024-10-05 08:45:55

这是一个函数指针。您会收到 SEGMENTATION FAULT,因为您尝试调用地址无效 (NULL) 的函数。

根据您的具体示例,该函数不应返回任何值 (void),并且不应接收任何参数 ()

这应该可行:

void a()
{
    printf("Hello!");
}

int main(int arcg, char** argv)
{
    m_process = a;
    m_process(); /* indirect call to "a" function, */
    // Hello!
}

函数指针通常用于 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:

void a()
{
    printf("Hello!");
}

int main(int arcg, char** argv)
{
    m_process = a;
    m_process(); /* indirect call to "a" function, */
    // Hello!
}

Function pointers are commonly used for some form of event handling in C. It's not its only use though...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文