What does "typedef void (*Something)()" mean

C++TypedefStatic MembersVoid Pointers

C++ Problem Overview


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?

C++ Solutions


Solution 1 - 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.

Solution 2 - C++

It introduces a function pointer type, pointing to a function returning nothing (void), not taking any parameters and naming the new type MCB.

Solution 3 - C++

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.

Solution 4 - 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...

Solution 5 - C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDogDogView Question on Stackoverflow
Solution 1 - C++Jonathan LefflerView Answer on Stackoverflow
Solution 2 - C++Jim BrissomView Answer on Stackoverflow
Solution 3 - C++Oliver CharlesworthView Answer on Stackoverflow
Solution 4 - C++Pablo Santa CruzView Answer on Stackoverflow
Solution 5 - C++Krishna Kanth YenumulaView Answer on Stackoverflow