Click here to Skip to main content
15,400,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My question:

I want to initialize dynamic malloc pointer to function pointers, and initialize this pointer the multiple elements in one line instead of initializing each one in a sperate line.

What I have tried:

Hi,

Can I do this:
C
typedef bool (*FPTR)();

bool function1(void){printf("function1");}
bool function2(void){printf("function2");}

int main(){

    // allocate & initialize tasks functions pointers
    FPTR *cr1_task_fptr = malloc(2*sizeof(FPTR));
    (cr1_task_fptr) = (FPTR){&function1, &function2}; // this is the issue cause

    printf("%p\n",&function2);            // function address
    printf("%p\n",(cr1_task_fptr+1));     // prints not the same function address


    return 0;
}

I also get this warning:
warning: excess elements in scalar initializer
Posted
Updated 28-Aug-21 7:51am
v2
Comments
Rick York 28-Aug-21 14:16pm
   
Peter is correct. You can't do that. What I wonder is why you would want to. Two function pointers use a very small amount of memory so why would you want to dynamically allocate that? You will use just as much if not more memory in the allocation table for that entry so what's the point? I recently wrote a thread event handler dispatch mechanism which was a table of 64 entries and it was not dynamically allocated because a) there is no good reason for it to be and b) it can't be any larger than it is because a thread can wait for at most 64 objects at once in windows.
R1S8K 28-Aug-21 20:53pm
   
Thanks for you comment.

1. I actually want to run a task manager for embedded programming. So to check the functions in the task manager library, I have to store the functions' addresses in functions pointers.
2. But the big problem now is that how to get the arguments for each function ?
3. Could you please send me the link for your project ?

1 solution

The compiler isnt lucky that you havent declared an array. This should work.
C
FPTR cr1_task_fptr[2] = (FPTR){&function1, &function2};
// else may work this
cr1_task_fptr[0] = &function1;
cr1_task_fptr[1] = &function2;
tip: use the debugger and memory view to explore the details
   
Comments
R1S8K 29-Aug-21 12:14pm
   
So, is it the same to pass array vs pass with a pointer ?
Now, where would I need a pointer ? And why I'm using dynamic allocation ?

--------------------------------------------------------------------------

My project briefing:
I want to develop a task manager that receive the input settings about how many tasks in the application + list of arguments for each task.

The struct for my task manager done with coroutine strategy:

// type defs
typedef enum {NOT_FINISHED, FINISHED}STATE;
typedef bool (*FPTR)();
typedef void ARGS;
// variables

// structs
typedef struct __attribute__((__packed__)) COROUTINE{
STATE c_st; // Coroutine state
uint8_t t_cts; // task counts
uint8_t t_ctr; // task counter
} COROUTINE;


I edited this struct which was before like this:

// type defs
typedef enum {NOT_FINISHED, FINISHED}STATE;
typedef bool (*FPTR)();
typedef void ARGS;
// variables

// structs
typedef struct __attribute__((__packed__)) COROUTINE{
uint8_t t_cts; // task counts
uint8_t t_ctr; // task counter
STATE c_st; // Coroutine flag states
STATE *t_st; // Coroutine tasks states
FPTR *t_fp; // Coroutine function pointer
ARGS *t_args; // pointer to list of tasks args
} COROUTINE;


So, I thought why would I have to allocate memory for the tasks and the linked state for each task ? So I removed the function pointer and state struct members.


But now I have to redesign the code and think of a way to call the functions and pass the required arguments.

My main goal is to manager the call of functions based on whether to run them in a thread, sequentially or in round-robin multitask.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900