55

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?

DogDog
  • 4,820
  • 12
  • 44
  • 66
  • 12
    Hey - are you talking about C or C++? Your .C file indicates C++; your tags indicate C. Make your mind up - there is a big difference between the two languages here. – Jonathan Leffler Oct 20 '10 at 21:36
  • 7
    This is definately C++. `.C` is a common C++ file extension, and furthermore `Modes::m_process` would be invalid C. – SingleNegationElimination Oct 20 '10 at 22:48
  • The author has clearly specified the tag, he asked for C, and he has edited it multiple times to C. Whether he initially confused some code with other, is another discussion. The answer is right, answering a C snippet. – another Oct 19 '17 at 10:24

5 Answers5

55

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    I'd vote for the code being compiled as C++, due to the second line in the original question using the scope resolution operator. Still, +1 for being technically correct. – Jim Brissom Oct 20 '10 at 21:36
  • 1
    I deleted mine, since it is indeed tagged C. I was looking at the code, though so I thought C++. Yours covers both, anyway, so +1 to that. – GManNickG Oct 20 '10 at 21:41
  • 1
    @GMan: I understand the confusion! – Jonathan Leffler Oct 20 '10 at 21:44
7

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

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
  • 1
    If we have a function like void fun(int a), then kk=fun. Now kk poinits to the function fun(int a). You can call the function using kk(4). We can use kk=&fun too. Reference : https://cs.nyu.edu/courses/spring12/CSCI-GA.3033-014/Assignment1/function_pointers.html – Krishna Kanth Yenumula Jul 11 '19 at 09:32
6

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

Jim Brissom
  • 31,821
  • 4
  • 39
  • 33
  • 1
    No - it takes unspecified parameters (because the question is tagged C), not zero parameters (as it would if it were tagged C++). – Jonathan Leffler Oct 20 '10 at 21:33
  • Yes, and no. See my comment to your answer as to why I think answering in that specific way (and probably re-tagging the question) is in order. – Jim Brissom Oct 20 '10 at 21:39
  • I understand and agree/sympathize - I've edited my answer to allow for the probable retagging, and to point out that this time it *really* matters whether the question is about C or C++. – Jonathan Leffler Oct 20 '10 at 21:42
5

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.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
4

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

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292