Pointers clarification in makecontext function - c

I have been implementing a user threads library as part of my assignment.
I didn't understand the makecontext function:
makecontext(&(mainthread->threadctx),(void(*)(void))start_funct,1,args)
What does (void(*)(void))start_funct exactly mean? And why do I have to write it this way?
Can't I just write it as
makecontext(&(mainthread->threadctx),start_funct,1,args) ?
Please be patient with me, I am not yet comfortable with pointers :)

void(*)(void) means "pointer to a function that takes no parameters and returns void".
Therefore (void(*)(void))start_funct is casting start_funct (which we can assume is some kind of function pointer)` to the above type. (There is a very useful online tool that can help you with this until you get more comfortable reading declarations).
You have to write it this way because the signature of start_funct is not void start_funct(void), so casting is required.

Related

How to declare a function if it's return value and parameters are given in command line and unknown before implementation? [duplicate]

I am presently in a case where I need to call a lot of function pointers that has been extracted at runtime. The problem is that the arguments are unknown at compilation time.
But, at runtime I receive datas that allows me to know the arguments of the function and I can even store the arguments in a char* array. The problem is that I don't have a function pointer model to cast it into.
In high level language, I know there is function like "InvokeMethode(String name,Byte[] args)" that interpret the bytes array like arguments. Since reflection does not exist in C, I have no hope to see this with a function pointer.
One solution that I have in mind (and it's really bad), is to create a model of function pointer at compilation time that will cast in a "hardcoded way" the ptr to the right type to use like this:
void callFunc64Bits(void* funcPtr,long long args);
void callFuncVoid(void* funcPtr);
The problem is that I will have to create like 100 function like this that will cast the pointer correctly.
Is there a way to do it more efficiently?
Thank you very much!
This is a hard problem without, unfortunately, good or easy answers.
See this former SO question: Run-time parameters in gcc (inverse va_args/varargs)
See this C FAQ question: http://c-faq.com/varargs/invvarargs.html
See this collection of "wacky ideas" by the C FAQ list maintainer: http://c-faq.com/varargs/wacky.html
Addendum: see this former SO question: How to call functions by their pointers passing multiple arguments in C?
...which mentions "libffi": http://sourceware.org/libffi/

C late binding with unknown arguments

I am presently in a case where I need to call a lot of function pointers that has been extracted at runtime. The problem is that the arguments are unknown at compilation time.
But, at runtime I receive datas that allows me to know the arguments of the function and I can even store the arguments in a char* array. The problem is that I don't have a function pointer model to cast it into.
In high level language, I know there is function like "InvokeMethode(String name,Byte[] args)" that interpret the bytes array like arguments. Since reflection does not exist in C, I have no hope to see this with a function pointer.
One solution that I have in mind (and it's really bad), is to create a model of function pointer at compilation time that will cast in a "hardcoded way" the ptr to the right type to use like this:
void callFunc64Bits(void* funcPtr,long long args);
void callFuncVoid(void* funcPtr);
The problem is that I will have to create like 100 function like this that will cast the pointer correctly.
Is there a way to do it more efficiently?
Thank you very much!
This is a hard problem without, unfortunately, good or easy answers.
See this former SO question: Run-time parameters in gcc (inverse va_args/varargs)
See this C FAQ question: http://c-faq.com/varargs/invvarargs.html
See this collection of "wacky ideas" by the C FAQ list maintainer: http://c-faq.com/varargs/wacky.html
Addendum: see this former SO question: How to call functions by their pointers passing multiple arguments in C?
...which mentions "libffi": http://sourceware.org/libffi/

Unfamiliar C syntax?

I am trying to understand C code written by others and have come across a piece of code that I do not understand all of the syntax and the parts I don't understand I don't know how to search for. I would appreciate either an explanation or information on how to search this and other similar things I may encounter, or both. The code in question is the following line:
int r = uv_listen((uv_stream_t*) &server, 128, on_new_connection);
I understand that r is a variable that is being declared and initialized to the value returned by the function "uv_listen()", 128 is a literal int paramater, on_new_connection has to be a function pointer since that is the name of a call back function that is called. server is a variable of a custom type (uv_tcp_t) and with the & it is referring to the address of server. What I don't understand is the "(uv_stream_t*) &server". It looks like this is one of the parameters to the function. I could understand a function call that returned a value as a parameter but this doesn't look like a function call. "uv_stream_t" is another custom type that is defined in their code.
I don't know if it is helpful in understanding what it means but the line of code is from sample code written to help in understanding how to use libuv.
It's just a type cast. &server gives the address of the server variable and (uv_stream_t*) casts the type of that address.
Based on the other information in your post, it seems that server is of type uv_tcp_t, but uv_listen wants a pointer to uv_stream_t. That's why you take the address of server and cast it to uv_stream_t*.
Note: this only makes sense because of how libuv defines uv_tcp_t and uv_server_t — in general you can't just cast pointer types to other pointer types and expect anything reasonable to happen.
(uv_stream_t*) &server is taking the server address and casting it to a uv_stream pointer.
&server - take address of server
(uv_stream*) - cast to uv_stream pointer
It's a type cast operator, used to tell the compiler to accept &server as a pointer to a uv_stream_t.
Presumably the type of 'server' is not uv_stream_t or a subclass, but the coder knows that it's safe to be treated as one.

Template in ansi C?

How I can create function with other types of data (some struct or sth)? In C++ exist templates, but in C?
I hear about void *, but i dont know if it works.
Any ideas?
Well, the way to do it is with void *. You might also need use function pointers, for example if you need to compare generic values.
The other way to do it is to use xmacros, but that's generally more for reducing code duplication for very similar structures.
void * is the solution in C as any pointer has the same sizeof() as void *. Of course, you get no type safety, but it's as good an abstraction as you can get with C. Furthermore, you could look at stdarg.h and variadic functions, but again, you should keep track yourself of what you're doing, since the compiler won't aid you one bit.

What does this C statement do?

Folks, my professor has assigned us an assignment involving typing with arrays, pointers, and functions. Now, before you jump on my back with the whole "We aren't going to do your homework for you" thing, the answer to my question will not solve the problem. In fact, I've already taken my best guess at the answer. I am just curious to hear what you think about this.
Here is the C statement I was given:
double(*foo(double(*)(double,double[]),double))(double, ...);
Our problem involved describing the type of foo. My question is simply this: What on earth does this statement do? As far as I can read it, this is either one of the most obfuscated and unrealistic lines of code I've ever seen, or it's not actually valid C. Let me know what you think.
What you want to learn is the Clockwise Spiral Rule. Learning this will help you describe, in plain words, what any type in C is. Learn it well.
This will help a little
typedef RETURNTYPE (* POINTER_TO_FUNCTION_TYPE)(ARGTYPE1, ARGTYPE2)
C supports a type that is a pointer to function, and you can typedef one this way to simplify longer expressions based on it.
So, with this typedef, I could declare this function
POINTER_TO_FUNCTION_TYPE f(POINTER_TO_FUNCTION_TYPE g);
It's a function that takes a pointer to function and returns a pointer to a function. Without the typedef, imagine how that declaration would look.
According to my compiler (Visual Studio 2010) the statement does absolutely nothing. It compiles successfully, but there are no assembly instructions generated for it, so the debugger steps right over it. As for why it does nothing and what it really means - it would take someone with more knowledge of C than me to explain that.
I can figure out the type of foo using the clockwise/spiral rule, though - thanks, jer! It's easier if you reformat it a bit:
double
(
*foo
(
double(*)(double, double[]),
double
)
)
(double, ...);

Resources