I have three function arrays each pointing to a number of functions.
I can call any of those functions form the three tables.
Now I would like to dereference the three arrays into a single array of function pointers but I just can't get it working!
void afunc1(void);
void afunc2(void);
void afunc3(void);
void bfunc1(void);
void bfunc2(void);
void bfunc3(void);
void cfunc1(void);
void cfunc2(void);
void cfunc3(void);
void(*FuncTbla[])(void) = { afunc1, afunc2, afunc3 };
void(*FuncTblb[])(void) = { bfunc1, bfunc2, bfunc3 };
void(*FuncTblc[])(void) = { cfunc1, cfunc2, cfunc3 };
void (*AllFuncTbls[])(void) = { &FuncTbla, &FuncTblb, &FuncTblc };
int TblNo = 1, FuncNo = 1; // tblNo 1 = table b
bFunc2(); // calls bFunc2 directly
FuncTblb[FuncNo](); // Calls Function bFunc2 via function table b
// Call same function using table of function tables
AllFuncTbls[TblNo][FuncNo](); // Does not compile - expression must be a pointer to a complete object type!!!
Two things: First of all remember that arrays naturally decays to pointers to their first element; And secondly it will become so much easier if you use type-aliases for the function types.
Armed with that knowledge you could do it like e.g.
// Type-alias to simplify using function pointers
typedef void (*function_type)(void);
// The three tables
function_type FuncTbla[] = { &afunc1, &afunc2, &afunc3 };
function_type FuncTblb[] = { &bfunc1, &bfunc2, &bfunc3 };
function_type FuncTblc[] = { &cfunc1, &cfunc2, &cfunc3 };
// A table of pointers to the first elements of each array
function_type *AllFuncTbls[] = { FuncTbla, FuncTblb, FuncTblc };
To call a function using AllFuncTbls is as simple as
AllFuncTbls[TblNo][FuncNo]();
If you use typedefs it works:
void afunc1(void);
// ...
typedef void (*funcPtr)(void);
// void(*FuncTbla[])(void) = { afunc1, afunc2, afunc3 };
// ...
funcPtr FuncTbla[] = { afunc1, afunc2, afunc3 };
funcPtr FuncTblb[] = { bfunc1, bfunc2, bfunc3 };
funcPtr FuncTblc[] = { cfunc1, cfunc2, cfunc3 };
//void (*AllFuncTbls[])(void) = { &FuncTbla, &FuncTblb, &FuncTblc };
funcPtr* AllFuncTbls[] = { FuncTbla, FuncTblb, FuncTblc };
// Use an Array of pointers to function pointers here, not an array of function pointers!
// ...
// Call same function using table of function tables
AllFuncTbls[TblNo][FuncNo](); // Compiles now
I commented out the lines that had to be changed.
Using typealiases is better approach, but if you were courious how to do it without it:
void(**AllFuncTbls[])(void) = { FuncTbla, FuncTblb, FuncTblc};
Related
I use the Aeron client implementation in C. And I need to make closure. But there are no closures in C...
For example:
void poll_handler(void *clientd, const uint8_t *buffer, size_t length, aeron_header_t *header)
{
aeron_subscription_t* subscription = (aeron_subscription_t*)clientd;
aeron_subscription_constants_t subscription_constants;
aeron_header_values_t header_values;
aeron_subscription_constants(subscription, &subscription_constants)
aeron_header_values(header, &header_values);
// How to get some_data here?
}
void some_func(int some_data)
{
aeron_fragment_assembler_create(fragment_assembler, poll_handler, subscription)
}
I tried using Clang blocks extension, but there is no function implementation that accepts block.
How can I get some local data in handler?
The aeron_fragment_assembler_create function takes a pointer to any structure as the third parameter. It doesn't need to be the aeron_subscription_t*.
You can create your own structure, initialize it with the necessary data and pass a pointer to it. And for convenience, you can save a pointer to aeron_subscription_t in the structure.
More or less like this:
typedef struct some_data_t {
aeron_subscription_t* subscription;
// ...
} some_data_t ;
void poll_handler(void *clientd, const uint8_t *buffer, size_t length, aeron_header_t *header)
{
some_data_t * some_data = (some_data_t *) clientd;
aeron_subscription_t* subscription = some_data->subscription;
// ...
}
void some_func(some_data_t* some_data)
{
aeron_fragment_assembler_create(fragment_assembler, poll_handler, some_data)
}
Summary:
I have an issue where my pointer inside a struct gets randomised after being passed to the function.
So I pass the original struct with the pointer being in-tact (I checked it there and it works), but after being passed to the function the stated pointer doesn't work anymore. The pointer points to the same address, but the content of the struct is lost and randomised without any prior data still existing.
Note: All of the signatures like ph_ReturnTypeInt are just specialised types aka. structs where I added additional data which don't matter much in this case, except for the function pointer signatures
Note 2: Since it's a lot of code that might be unimportant I tried to explain what is what, but here the GitHub link if you need it. Else thank you if you can help me ^^
The function being called:
/// Defined wrapper for the function
/// #param call_ctx Call Context for the wrapper
/// #param x Example for how a user argument could look like
ph_ReturnTypeInt DecorateFunc_Wrapper(DecorateFunc_WrapContext *call_ctx, int x)
{
printf("Called wrapper\n");
// ----> Compiler generated ---->
ph_ReturnTypeInt call_r;
// Child Context is null -> Reached lowest level of wrapping
if (!call_ctx->child_ctx && !call_ctx->has_child_ctx)
{
// Calling the wrapped function
call_r = call_ctx->wrapped_func(x);
}
else
{
// Passing the context down one level to the other function
call_r = (*call_ctx->child_ctx).wrapper_func(call_ctx->child_ctx, x);
}
int local_r = call_r.actual_value;
// <---- Compiler generated <----
printf("Finished function call\n");
// ----> Compiler generated ---->
ph_ReturnTypeInt func_r = {
.base.is_exception = false,
.base.is_null = false,
.actual_value = local_r
};
// <---- Compiler generated <----
return func_r;
}
The struct which "loses" its child_ctx pointer:
/// Context for the DecorateFunc Decorator. Contains a child_ctx element to point to a child if it exists. Contains
/// a wrapper function and wrapped function. The wrapped function should be NULL if child_ctx is populated.
typedef struct DecorateFunc_WrapContext {
bool has_child_ctx;
ph_DecoType_Int_Int wrapped_func;
DecorateFunc_Wrapper_Type wrapper_func;
DecorateFunc_WrapContext *child_ctx;
} DecorateFunc_WrapContext;
Function that returns the struct:
/// Decorates a function and returns a struct containing the func and the wrapper specified for this decorator.
/// #param passable Passable struct that can either contain a function or an initialised wrapped struct that should
/// be wrapped again. In both cases the types must match with the target of the decorator to correctly pass
/// the arguments.
DecorateFunc_WrapContext DecorateFunc(DecorateFunc_WrapContext ctx)
{
printf("Called decorator\n");
// ----> Compiler generated ---->
DecorateFunc_WrapContext new_ctx;
// Child Context is null -> Reached lowest level of wrapping / The function does not have any more wrapping
if (!ctx.child_ctx && !ctx.has_child_ctx && !ctx.wrapper_func)
{
new_ctx = (DecorateFunc_WrapContext) {
.has_child_ctx = false,
.wrapper_func = DecorateFunc_Wrapper,
.wrapped_func = ctx.wrapped_func,
.child_ctx = NULL
};
}
else
{
// Creating a new context and passing the context as a child
new_ctx = (DecorateFunc_WrapContext) {
.has_child_ctx = true,
.wrapper_func = DecorateFunc_Wrapper,
.child_ctx = &ctx,
};
}
// <---- Compiler generated <----
return new_ctx;
}
The main function:
int main()
{
DecorateFunc_WrapContext p;
p = (DecorateFunc_WrapContext) { .wrapped_func = &main_func };
DecorateFunc_WrapContext deco_ctx = DecorateFunc(p);
deco_ctx.wrapper_func(&deco_ctx, 15);
/* Wrapping the wrapped context */
DecorateFunc_WrapContext deco_ctx2 = DecorateFunc(deco_ctx);
deco_ctx2.wrapper_func(&deco_ctx2, 20);
}
The function passed as function pointer:
ph_ReturnTypeInt main_func(int x)
{
printf("Called decorated function - Passed argument: %i\n", x);
/* Compiler generated return */
ph_ReturnTypeInt r = {
.base.is_exception = false,
.base.is_null = false,
.actual_value = 3
};
return r;
}
And lastly the additional context (the main file and the other header with the signatures, which shouldn't have a big influence):
// Used content of the header. Other content is just declarations etc.
/* Undefined Base Return which serves as the base for all ReturnTypes */
typedef struct ph_UndefBaseReturn {
bool is_exception;
const char* exception;
const char* traceback;
bool is_null;
} ph_UndefBaseReturn;
/* Para-C Return of Type int. Compiler-Generated */
typedef struct ph_ReturnTypeInt {
ph_UndefBaseReturn base;
int actual_value;
} ph_ReturnTypeInt;
/* Decorator Return Types - Compiler-Generated */
typedef ph_ReturnTypeInt (*ph_DecoType_Int_Int)(int);
// At the top of the main file
typedef struct DecorateFunc_WrapContext DecorateFunc_WrapContext;
/// Signature of the wrapper - Returns int and contains as parameters a int return function and an int
/// This type will be automatically generated for any wrapper, but only used in the decorator for correctly creating
/// the struct which will store the wrapper and wrapped function.
typedef ph_ReturnTypeInt (*DecorateFunc_Wrapper_Type)(DecorateFunc_WrapContext*, int); // R: int - P: struct, int
In main:
/* Wrapping the wrapped context */
DecorateFunc_WrapContext deco_ctx2 = DecorateFunc(deco_ctx);
deco_ctx2.wrapper_func(&deco_ctx2, 20);
In DecorateFunc:
DecorateFunc_WrapContext DecorateFunc(DecorateFunc_WrapContext ctx)
{
...
{
// Creating a new context and passing the context as a child
new_ctx = (DecorateFunc_WrapContext) {
.has_child_ctx = true,
.wrapper_func = DecorateFunc_Wrapper,
.child_ctx = &ctx, // <-- this line
};
}
}
The assignment to child_ctx at <-- this line links new_ctx to a temporary copy of deco_ctx in main(). Since you passed the structure by value, the compiler constructed a temporary copy of it on the stack, then (likely) re-used that area of the stack once the function completed. Your link (.child_ctx) is now dangling.
You need to pass the addresss of new_ctx, adjust DecorateFunc to accept a pointer, assign .child_ctx to that pointer, and adjust your tests to deal with a pointer, it works.
This question already has answers here:
Error "initializer element is not constant" when trying to initialize variable with const
(8 answers)
Closed 3 years ago.
Here i am trying to make state machine where FsmHdlr should call a appropriate function based on state and event. What i am getting the above error. How to resolve this.
S16 handleParamReqEvt(void)
{
/* doing something */
RETVALUE(ROK);
}
S16 handleParamRspEvt(param_resp *paramRsp)
{
/* doing something */
RETVALUE(ROK);
}
typedef enum{
IDLE,
CONFIGURED,
MAX_STATE
}STATE;
/* Events in CL */
typedef enum{
PARAM_REQ,
PARAM_RSP
MAX_EVENT
}EVENT;
param_resp *paramMsg;
S16 FsmHdlr[MAX_STATE][MAX_EVENT] =
{
{
/* PHY_STATE_IDLE */
handleParamReqEvt(), //error :initializer element is not constant
handleParamRspEvt(paramMsg) //error: initializer element is not constant
}
};
It sounds like you don't actually want to call the function during initialization (which you can't do for static storage variables anyway, as you've discovered). It sounds like you are building a dispatch table. What you want is called a function pointer.
This how one uses function pointers:
int foo(void) { ... }
int main(void) {
int (*bar)(void) = foo;
bar(); // Calls foo
}
Since the parameters vary based on the type of the event, a 2d array doesn't make much sense. I'd use the following in your case:
S16 fsm_hdrl_idle_param_req(void) { ... }
S16 fsm_hdrl_idle_param_rsp(ParamRsp *param_rsp) { ... }
S16 fsm_hdrl_conf_param_req(void) { ... }
S16 fsm_hdrl_conf_param_rsp(ParamRsp *param_rsp) { ... }
typedef S16 (*FsmReqHdlr)(void);
typedef S16 (*FsmRspHdlr)(ParamRsp*);
typedef struct {
FsmReqHdlr fsm_req_hdlr;
FsmRspHdlr fsm_rsp_hdlr;
} FsmHdlrs;
FsmHdlrs fsm_hdlrs_by_state[MAX_STATE] = {
{ fsm_hdrl_idle_param_req, fsm_hdrl_idle_param_rsp },
{ fsm_hdrl_conf_param_req, fsm_hdrl_conf_param_rsp },
};
Later:
fsm_hdlrs_by_state[state].fsm_req_hdlr();
fsm_hdlrs_by_state[state].fsm_rsp_hdlr(param_rsp);
You could declare pointer to FsmHdlr[MAX_STATE][MAX_EVENT] as a global variable.
S16 (*FsmHdlr)[MAX_STATE][MAX_EVENT] = NULL;
Somewhere in main function allocate the memory to the global pointer as below.
FsmHdlr = malloc(sizeof(S16 [MAX_STATE][MAX_EVENT]));
Then use memcpy to copy array compound literal as below.
memcpy(FsmHdlr,
(S16 [MAX_STATE][MAX_EVENT]) {
{
/* PHY_STATE_IDLE */
handleParamReqEvt(), handleParamRspEvt(paramMsg)
}
},
sizeof((S16 [MAX_STATE][MAX_EVENT]) {
{
/* PHY_STATE_IDLE */
handleParamReqEvt(), handleParamRspEvt(paramMsg)
}
})
);
And dereference as below,
(*FsmHdlr)[0][0];//to access 1st element
(*FsmHdlr)[0][1];//to access 2nd element
You cannot initialise array during declaration using values, what are unknown while compilation.
And, your initialization values are values returned by handleParamReqEvt() and handleParamRspEvt(...), what are unknown.
I suppose that you are thinking about pointers to these functions, nor function values.
So, you should use function names instead of function calls, like below:
S16 FsmHdlr[MAX_STATE][MAX_EVENT] =
{
{
/* PHY_STATE_IDLE */
handleParamReqEvt, //initializer element is NOW constant
handleParamRspEvt //initializer element is NOW constant
}
};
Unfortunately, this will not compile because of wrong array type - now it is not S16[][] (or S16**) like before.
Additionaly, both pointers are pointers of different types:
- first is S16 ( * )();
- second is S16 ( * )(param_resp*);
Fortunately, you can store them both as void* (pointer to anything), but remember that you MUST PROPERLY CAST them before usage.
For cast simplification you can declare the types of these function using typedef directive.
So, the final form of declaration+initialization and usage will be:
// declaration + initialisation of array
void* FsmHdlr[MAX_STATE][MAX_EVENT] =
{
{
/* PHY_STATE_IDLE */
handleParamReqEvt,
handleParamRspEvt
}
};
// declaration of types
typedef S16 (*reqEvt_ptr)(); //signature of handleParamReqEvt()
typedef S16 (*rspEvt_ptr)(param_resp*); //signature of handleParamRspEvt(param_resp*)
// usage:
// handleParamReqEvt
reqEvt_ptr reqEvt = (reqEvt_ptr)FsmHdlr[/* index here */][PARAM_REQ]; // cast
S16 reqResult = reqEvt(); // call
// handleParamRspEvt
rspEvt_ptr rspEvt = (rspEvt_ptr)FsmHdlr[/* index here */][PARAM_RSP]; // cast
S16 rspResult = rspEvt(/* pointer to paramMsg here */); // call
I don't have much experience in Object oriented programming.I am trying to create an object in c which will have its own methods.
I have declared structure which have pointers to function. All instance of this variable are going to point same function. But currently I need to initialize every instance of variable as in main (Line 1 and Line 2). So is there any method that will initialize its default value when I declare it?
#include <stdio.h>
#include <stdlib.h>
typedef struct serialStr Serial;
struct serialStr
{
void(*init)(Serial*);
void(*open)();
void(*close)();
};
void open()
{
printf("Open Port Success\n");
return;
}
void close()
{
printf("Close port Success\n");
return;
}
void init(Serial* ptr)
{
ptr->open = open;
ptr->close = close;
}
int main()
{
Serial serial,serial_2;
serial.init = init;
serial.init(&serial); // Line1
serial_2.init = init;
serial_2.init(&serial_2); // Line2
serial.open();
//rest of code
serial.close();
serial_2.open();
serial_2.close();
return 0;
}
In C, the standard way would be to declare an initializer macro:
#define SERIAL_INITIALIZER { .init = init, .open = open, /* and others */ }
Serial serial = SERIAL_INITIALIZER;
In most cases in C there is simply no need for dynamic intialization of variables. You only need it for malloced objects.
C++ add some automatization by calling constructor/destructor. In pure C is no way to do so. You should do all steps manually: create and initialize object (call constructor-like function for structure), call functions by pointers from the structure instance, call destructor (it should destroy the instance and free related resources).
If is no polymorphism in your task then use simple way - without pointers to functions, but each function (method) should take pointer to the object.
Common case example:
struct MyStruct
{
// data
};
struct MyStruct* createMyStruct(/* maybe some input */)
{
// create, init and return the structure instance
}
void destoyMyStruct(struct MyStruct* obj)
{
// free resources and delete the instance
}
void doSomeAction(struct MyStruct* obj /* , some other data */)
{
// ...
}
int main()
{
struct MyStruct* object = createMyStruct();
doSomeAction(object);
destoyMyStruct(object);
return 0;
}
Edit 1: macro is only for very simple cases and error-prone way.
Typically, you would do this through "opaque type". Meaning that you declare an object of incomplete type in your header:
typedef struct Serial Serial;
And then in the C file, you place the actual struct definition. This will hide the contents of the struct to the caller (private encapsulation). From your constructor, you could then set up private member functions:
struct Serial
{
void(*init)(void);
void(*open)(void);
void(*close)(void);
};
// private member functions:
static void open (void);
...
// constructor:
Serial* SerialCreate (void)
{
Serial* s = malloc(sizeof (*s));
...
s->open = open;
return s;
}
This means that if you wish to inherit the class, you will only need to change the constructor.
Though of course, if you wish to implement true polymorphism, you don't want to change any code. You could solve this by passing the init function as parameter to the constructor.
header file:
typedef void init_func_t (void);
c file:
// constructor:
Serial* SerialCreate (init_func_t* init)
{
Serial* s = malloc(sizeof (*s));
...
init();
return s;
}
And then from the init function in the inherited class, set all private member functions.
I'm trying to define an array of function pointers, where each function contains an int parameter. I'm also trying to set the value of that int parameter in the array declaration
So I have a TIMED_TASK struct, that will hold the function pointer and value I want to pass
typedef struct
{
void (*proc)(int);
int delayMsec;
} TIMED_TASK;
Then I have an array of TIMED_TASKs like this
static const TIMED_TASK attractSequence[] =
{
{ LightsOn, 1000 },
{ LightsOff, 500 },
{ EndSequence, 0 }
};
And I'd like it to call each of those functions in turn, passing the delay value to each function. This is where I expect I have the wrong syntax (I'm still learning C). I seemingly don't hit my LightsOn() routine at all
void loop() // It's an arduino project :)
{
attractSequence[sequence];
sequence++;
}
void LightsOn(int pause)
{
// I do not hit this routine for some reason?
Serial.print("LIGHTS ON");
Serial.print(pause);
}
void LightsOff(int pause)
{
Serial.print("LIGHTS OFF");
Serial.print(pause);
}
It's entirely possible I'm taking the wrong approach here, but hopefully you can see what I'm trying to achieve. Any advice very welcome!
If you want to go through each item of attractSequence only once, you can use:
void loop()
{
int count = sizeof(attractSequence)/sizeof(attractSequence[0]);
for (int i = 0; i < count; ++i )
{
attractSequence[i].proc(attractSequence[i].delayMsec);
}
}