0

I'm trying to understand a code which have the following lines:

void terminate_pipe(int);
code code code...
struct sigaction new_Sigiterm;
new_Sigiterm.sa_handler = terminate_pipe;

My question are:

  • What is the meaning of calling a function like this? Is it going to just put NULL as the parameter?

  • It is void, so new_Sigiterm.sa_handler will be NULL no matter what?

thanks.

tijko
  • 7,599
  • 11
  • 44
  • 64
Gum Bi
  • 313
  • 2
  • 4
  • 11
  • 1
    The function isn't called; a function name without parentheses typically means you're dealing with a function _pointer_. The code in your question simply assigns the address of `terminate_pipe` to the `sa_handler` field in `struct sigaction`. See here for more info: http://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work – user4520 Dec 04 '15 at 21:29
  • I dont think this is the case here. the function is: void terminate_pipe(int){ *code* } its jus the signature of the function. why do you think it is function pointer? – Gum Bi Dec 04 '15 at 21:31
  • It is indeed. Please take a look at the question (and answers) I've linked you to. – user4520 Dec 04 '15 at 21:33
  • Im sorry, im a java programmer and im very confused now. the sa_handler field is... a function field? and the code is "overriding" it? – Gum Bi Dec 04 '15 at 21:36
  • As far as I know, Java really doesn't have anything similar, but in C# the equivalent would be delegates. In general the `sa_handler` field of `struct sigaction` contains an _address_ of a function, so you can call that function at any time. `new_Sigiterm.sa_handler = terminate_pipe;` simply causes that field to point at `terminate_pipe`. – user4520 Dec 04 '15 at 21:38
  • i could name the function in any name? or i am overriding the specific function "terminate_pipe" of sa_handler? – Gum Bi Dec 04 '15 at 21:46

3 Answers3

3

new_Sigiterm.sa_handler is most likely a pointer that points to a function. By running

new_Sigiterm.sa_handler = terminate_pipe;

It's similar to saying

new_Sigiterm.sa_handler = &terminate_pipe;

(Like in pointers). This is not running the function, it's just making a pointer that points to the function, if you "run" the pointer, the pointed function will run.

This is how to declare function pointer:

void function(int x);

int main()
{
    //Pointer to function
    void (*foo) (int);

    //Point it to our function
    foo = function;

    //Run our pointed function
    foo(5);
}

More info about function pointers

TomTsagk
  • 1,474
  • 9
  • 23
1

Code like this assignment is setting a handler (sometimes called a function pointer): Basically, the address of the function to run, at a given time.

The syntax for this in C is to name the function, but don't put () on the end. That returns the address of the function.

new_Sigiterm.sa_handler = terminate_pipe;
TakUnderhand
  • 111
  • 1
  • 6
  • Note that you could also do `&terminate_pipe`; no difference in this case. – user4520 Dec 04 '15 at 21:33
  • sa_handler field is a place where the address of the function can be put. – TakUnderhand Dec 04 '15 at 21:39
  • Im sorry, im a java programmer and im very confused now. the sa_handler field is... a function field? and the code is "overriding" it? – Gum Bi Dec 04 '15 at 21:40
  • Correct. You are changing the address that the program execution goes to. – TakUnderhand Dec 04 '15 at 22:03
  • At some stage, the execution of the program will look at the variable new_Sigterm.sa_handler. Hopefully that code will check for obviously wrong values (like NULL, or 0 (which are the same thing)). If there is a function to call (or something that looks like a function), then that function will be called. Hope it's not pointing to invalid memory somewhere :) Welcome to C. – TakUnderhand Dec 04 '15 at 22:07
0
  1. void terminate_pipe(int); is not calling of function it is Forward declaration of function.
  2. In new_Sigiterm.sa_handler sa_handler is a Function Pointer.
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42