Unfamiliar C syntax? - c

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.

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/

Will a cast ever fail in C?

Specifically, my question is, given this macro:
#define FAKE_VAL(type) ((type)0)
...is there any value of type (including structures, function pointers, etc.) where FAKE_VAL(type) will cause a compile-time error?
I'm asking because I have a macro that take a function pointer as an argument, and needs to find the size of its return value. I know the types and number of arguments the function pointer takes, so I'm planning to write something like:
sizeof(fptr(FAKE_VAL(arg_type_1), FAKE_VAL(arg_type_2)))
arg_type_1 and 2 could be literally anything.
Of course there is.
struct fred (i.e. not a pointer) - how do you convert 0 (scalar type) to a struct (non scalar type)?
Any literal value FAKE_VAL("hi") gives (("hi")0) - what does that mean?
You can't cast an int to an array type, so
FAKE_VAL(int[5]);
will fail. Try it!
To give it a more systematic answer. In C cast are only allowed for arithmetic and pointer types. So anything that is a struct, union or array type would lead to a compile time error.
typecast is used to inform the compiler that the programmer has already considered the side effects of using the variable in a different way than it was declared as. This causes the compiler to switch off its checks. So with typecasts you wont get warnings/errors.
Yeah. it does type conversion of any type to the typecasted one. My bad.
But this can cause segmentation faults/errors when the program is actually run.

Filter type undeclared?

I should cast an Interrupt Filter to pass to a function, but the thing is, my Xcode always tells me that the Filter type is undeclared..
Here is part of my code:
interruptSrc = IOFilterInterruptEventSource::filterInterruptEventSource((OSObject*)this,
(IOInterruptEventAction)&VoodooSDHC::interruptHandler,
(Filter)&VoodooSDHC::interruptFilter,
(IOService*)provider);
I'm new to Mac and Xcode so I don't really know what should I do in this case.
How does it complain about it being not defined, when the function is actually taking a parameter of that type?
You're taking the address of something and casting it to something that doesn't look like a pointer. Either that's a pointer typedef (spawn of the devil) or that's not a good cast.
Are you sure it's not supposed to be:
(Filter *)&VoodooSDHC::interruptFilter,
?

Pointers clarification in makecontext function

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.

Resources