malloc to pointer of void and assign different data types

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

60 Replies - 3039 Views - Last Post: Today, 02:29 PM Rate Topic: ***** 1 Votes

#1 wolfrose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 20-January 16

malloc to pointer of void and assign different data types

Posted 27 March 2021 - 07:44 AM

Hi,

I need a reference pointer that has different data types; for example, 3 ints and one pointer to array.

I'm trying this:

const unsigned char PIC[] = {0xd0, 0xe0, 0xf0, 0x10, 0x20};

int main(){

    void *p;

    p = malloc(3*sizeof(unsigned char)+(1*sizeof(unsigned char*)));

    *(unsigned char *)(p+0) = 990;
    *(unsigned char *)(p+1) = 88;
    *(unsigned char *)(p+2) = 77;
    *(unsigned char *)(p+3) = &PIC;               // I know this isn't working !

    printf("%d\n",*(unsigned char *)(p+0));
    printf("%d\n",*(unsigned char *)(p+1));
    printf("%d\n",*(unsigned char *)(p+2));
    printf("%d\n",*(unsigned char *)(p+3));       // also i know this is not the right way to call array elements !
}
 


I think it worked for the uint8_t part but not with the array.

Any suggestions ?

Is This A Good Question/Topic? 0
  • +

Replies To: malloc to pointer of void and assign different data types

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7733
  • View blog
  • Posts: 25,851
  • Joined: 05-May 12

Re: malloc to pointer of void and assign different data types

Posted 27 March 2021 - 09:19 AM

Moving to C/C++ questions instead of the current C/C++ discussion...
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7733
  • View blog
  • Posts: 25,851
  • Joined: 05-May 12

Re: malloc to pointer of void and assign different data types

Posted 27 March 2021 - 09:26 AM

The easiest approach is just to cast the pointer to a struct that has the parts laid out how you want it. Here's an example of an int followed by a pointer to an array of integers
typedef struct
{
    unsigned int value;
    unsigned int * arr;
} MyStruct;

:

int myArray[12];
void * p = malloc(sizeof(MyStruct));
MyStruct * pStruct = p;
pStruct->value = 0xBEEF;
pStruct->arr = &myArray;


Was This Post Helpful? 1
  • +
  • -

#4 wolfrose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 20-January 16

Re: malloc to pointer of void and assign different data types

Posted 27 March 2021 - 09:59 AM

For example,

I want to assign these functions that have different argument settings:

  void         ssd1306_command(uint8_t c);
  boolean      getPixel(int16_t x, int16_t y);


The first function takes one argument of uint8_t, the second function takes two of int16_t.

So in my current project, I want to develop thread manager library.

For data structure for this project I have a struct for thread and struct for tasks.

I know this is not a working code, but I want to explain what I want to do in general.

header:
////////////////////////////////////////////////////////////////////////////////////////
// enumerations
typedef enum {NOT_FINISHED,FINISHED,DONE}STATE;
// variables
typedef void (*f_ptr)(void *args);

// structs
typedef struct __attribute__((__packed__)) TASK{
	STATE tsk_st;
	f_ptr *fptr;
	void *arg_list;
}TASK;


typedef struct __attribute__((__packed__)) THREAD{
	uint8_t tsk_cnts, tsk_cntr;     // task information
	STATE thrd_st;                  // thread flag states
    TASK *tasks;
}THREAD;




main:

void function1(void){printf("function1\n");}
void function2(uint8_t i){printf("function2# %d\n",i);}
void function3(uint8_t a, uint8_t b, const uint8_t *img){
    printf("a#%d b#%d a#0x%.2x\n",a,b,img[0]);}

const unsigned char PIC[] = {0xd0, 0xe0, 0xf0, 0x10, 0x20};

THREAD thread1;

int main(){

    // set thread main info
    thread1.thrd_st = 0;
    thread1.tsk_cnts = 4;
    thread1.tsk_cntr = 0;
        
    // store tasks addresses, I think I have to use malloc for this
    thread1.tasks.fptr[0] = function1;
    thread1.tasks.fptr[1] = function2;
    thread1.tasks.fptr[2] = function3;

    // store list of args for tasks, this one I guess requires malloc too
    thread1.tasks.arg_list = {1,2,3};

}


Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7733
  • View blog
  • Posts: 25,851
  • Joined: 05-May 12

Re: malloc to pointer of void and assign different data types

Posted 27 March 2021 - 11:02 AM

Well, so far from the code you've shown (or tried to show a pseudo code), it looks like you are still forced to know what each function pointer points to, so you have to hardcode the function call so that you can pass the right number of parameters, and types to it -- even if it is coming from the arglist. Since that is hardcode, you might as well hardcode that struct I alluded to above.

To get past that hardcoding, you need to have a way to encode in your data structure information like the number of arguments for each function, and the types of each argument. Furthermore you will have to have a function prototype for every permutation of the arguments. So unless you have a limited set of functions, this will become an exponentially infinite list of function prototypes. The way aroubd this to have calling conventions, but then to implement preparing for the function call and making the call will involve some assembly language that will be platform specific.
Was This Post Helpful? 1
  • +
  • -

#6 wolfrose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 20-January 16

Re: malloc to pointer of void and assign different data types

Posted 27 March 2021 - 03:37 PM

View PostSkydiver, on 27 March 2021 - 11:02 AM, said:

Well, so far from the code you've shown (or tried to show a pseudo code), it looks like you are still forced to know what each function pointer points to, so you have to hardcode the function call so that you can pass the right number of parameters, and types to it -- even if it is coming from the arglist. Since that is hardcode, you might as well hardcode that struct I alluded to above.


You're right. I think I might end in a closed route thinking that I could get away with function pointers to point to any function with different arguments configuration.

typedef void (*f_ptr)(void *args);


Well, this function pointer would receive only one argument.
But I think I read somewhere that I can do it like this:

typedef void (*f_ptr)();


I don't know what to do in this regard actually, maybe this also could work for max argument numbers in any library I'm working on.

typedef void (*f_ptr)(void *arg1, void *arg2, void *arg3, void *arg4);


So, if this eventually works, then I can pass what is necessary and skip the rest. How about this one.


Quote

To get past that hardcoding, you need to have a way to encode in your data structure information like the number of arguments for each function, and the types of each argument. Furthermore you will have to have a function prototype for every permutation of the arguments.


Yes, that general function pointer I have, is working and I can assign any function to it and I tested it on my Arduino microcontroller and it worked. I was able to call 4 functions and also pass the required arguments.

#include "task_manager.h"
#include "glcd_spi.h"
#include "arrays.h"


extern STATE lcd_st_flag; 

typedef void (*prgm_fptr)(void* arg);

void setup() {
  Serial.begin(9600);
  prgm_fptr glcd_fptr[4];

  glcd_fptr[0] = glcd_init;
  glcd_fptr[1] = glcd_graphics_mode;
  glcd_fptr[2] = glcd_clr;
  glcd_fptr[3] = glcd_img;

  lcd_st_flag = NOT_FINISHED;
  while(lcd_st_flag != FINISHED){
    glcd_fptr[0](0);
  }
  
  lcd_st_flag = NOT_FINISHED;
  while(lcd_st_flag != FINISHED){
    glcd_fptr[1](1);
  }
  
  lcd_st_flag = NOT_FINISHED;
  while(lcd_st_flag != FINISHED){
    glcd_fptr[2](0);
  }

  lcd_st_flag = NOT_FINISHED;
  while(lcd_st_flag != FINISHED){
    glcd_fptr[3](PIC3);
    //glcd_fptr[3](heart);
  }
}  





So after I ran this code, I was thinking going to the next level which is to write a multithreading library to manage calling functions from different libraries. I downloaded several threading libraries, but I'm focusing to write my own in C style.

Quote

So unless you have a limited set of functions, this will become an exponentially infinite list of function prototypes.


Yes of course, well, functions most the time not exceeding 10 functions in the library I'm working on right now.

Quote

The way around this to have calling conventions, but then to implement preparing for the function call and making the call will involve some assembly language that will be platform specific.


Let's hope that I don't need to go for the assembly route.

multithreading shouldn't be too difficult to do.

I'm just stuck now of one problem now.

In the next post, I tried to improve the code more. It should clear the problem more to you.

Header file:

#ifndef TASK_MANAGER_H_INCLUDED
#define TASK_MANAGER_H_INCLUDED
////////////////////////////////////////////////////////////////////////////////////////////
// libraries
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>


////////////////////////////////////////////////////////////////////////////////////////
// enumerations
typedef enum {NOT_FINISHED,FINISHED,DONE}STATE;
// variables
typedef void (*f_ptr)(void *args);

// structs
typedef struct __attribute__((__packed__)) TASK{
	STATE tsk_st;
	f_ptr *fptr;
	void *arg_list;
}TASK;


typedef struct __attribute__((__packed__)) THREAD{
	uint8_t tsk_cnts, tsk_cntr;     // task information
	STATE thrd_st;                  // thread flag states
    TASK *tasks;
}THREAD;


void init_thread(uint8_t _thrd_cnts);
void register_thread(THREAD thrd, uint8_t _tsk_cnts);
void run_thread(THREAD thrd);

#endif // TASK_MANAGER_H_INCLUDED






main.c:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include "task_manager.h"

void function1(void){printf("function1\n");}
void function2(uint8_t i){printf("function2# %d\n",i);}
void function3(uint8_t a, uint8_t b, const uint8_t *img){
    printf("a#%d b#%d a#0x%.2x\n",a,b,img[0]);}

const unsigned char PIC[] = {0xd0, 0xe0, 0xf0, 0x10, 0x20};

THREAD thread1;

int main(){

    // set thread main info
    thread1.thrd_st = 0;
    thread1.tsk_cnts = 4;
    thread1.tsk_cntr = 0;

    // allocating memory for tasks
    thread1.tasks->fptr = malloc(3*sizeof(f_ptr));

    // store tasks addresses
    thread1.tasks->fptr[0] = (f_ptr)function1;
    thread1.tasks->fptr[1] = (f_ptr)function2;
    thread1.tasks->fptr[2] = (f_ptr)function3;

    // allocating memory for tasks arguments list
    thread1.tasks->arg_list = malloc(2*sizeof(unsigned char)+(1*sizeof(unsigned char*)));



    // define the list of arguments
    *(unsigned char *)(thread1.tasks->arg_list+0) = 1;
    *(unsigned char *)(thread1.tasks->arg_list+1) = 2;
    *(unsigned char *)(thread1.tasks->arg_list+2) = &PIC;           //#1: is this correct ?

    // dereferencing the argument list
    printf("%d\n",*(unsigned char *)(thread1.tasks->arg_list+0));
    printf("%d\n",*(unsigned char *)(thread1.tasks->arg_list+1));
    printf("%d\n",*(unsigned char *)(thread1.tasks->arg_list+2));   //2# this isn't working

    return 0;
}




I'm marking #1 & #2 as the problems I'm having now.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7733
  • View blog
  • Posts: 25,851
  • Joined: 05-May 12

Re: malloc to pointer of void and assign different data types

Posted 27 March 2021 - 04:03 PM

Most OSes implement threads where there is simply a function pointer, and that function pointer takes a void pointer. It is up to the called function to determine what to do with that void pointer.
Was This Post Helpful? 1
  • +
  • -

#8 wolfrose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 20-January 16

Re: malloc to pointer of void and assign different data types

Posted 27 March 2021 - 04:38 PM

Yes, I agree with you, I really want to go through developing the library but the referencing/dereferencing of struct through a pointer to void of an array is stopping me now.

This post has been edited by Skydiver: 27 March 2021 - 04:56 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7733
  • View blog
  • Posts: 25,851
  • Joined: 05-May 12

Re: malloc to pointer of void and assign different data types

Posted 27 March 2021 - 04:56 PM

Why? It's a simple cast?
Was This Post Helpful? 1
  • +
  • -

#10 wolfrose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 20-January 16

Re: malloc to pointer of void and assign different data types

Posted 28 March 2021 - 11:24 AM

Yes, but I want to know how cast it right.

Index 0 & 1 also not working ! It worked with me before.

But it's okay, at least now I learned something about referencing to struct members of pointers to use -> operator.

Also only compiling the code takes a bit of time !

Quote

Process returned -1073741819 (0xC0000005) execution time : 1.736 s
Press any key to continue.


I think there's some thing fishy in the code.

OK, I commented all the code in main.c, and started uncommenting the lines from start:

These are ok:
    // set thread main info
    thread1.thrd_st = 0;
    thread1.tsk_cnts = 4;
    thread1.tsk_cntr = 0;



This is the cause of the delay in compile time:

    // allocating memory for tasks
    thread1.tasks->fptr = malloc(3*sizeof(f_ptr));




This is all the code now:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include "task_manager.h"


void function1(void){printf("function1\n");}
void function2(uint8_t i){printf("function2# %d\n",i);}
void function3(uint8_t a, uint8_t b, const uint8_t *img){
    printf("a#%d b#%d a#0x%.2x\n",a,b,img[0]);}

const unsigned char PIC[] = {0xd0, 0xe0, 0xf0, 0x10, 0x20};

THREAD thread1;

int main(){

    // set thread main info
    thread1.thrd_st = 0;
    thread1.tsk_cnts = 4;
    thread1.tsk_cntr = 0;

    // allocating memory for tasks
    thread1.tasks->fptr = malloc(3*sizeof(f_ptr));    // this cause compile delay

    // store tasks addresses
    //thread1.tasks->fptr[0] = (f_ptr)function1;
    //thread1.tasks->fptr[1] = (f_ptr)function2;
    //thread1.tasks->fptr[2] = (f_ptr)function3;

    // allocating memory for tasks arguments list
    //thread1.tasks->arg_list = malloc(2*sizeof(unsigned char)+(1*sizeof(unsigned char*)));



    // define the list of arguments
    //*(unsigned char *)(thread1.tasks->arg_list+0) = 1;
    //*(unsigned char *)(thread1.tasks->arg_list+1) = 2;
    //*(unsigned char *)(thread1.tasks->arg_list+2) = &PIC;           // is this correct ?

    // dereferencing the argument list
    //printf("%d\n",*(unsigned char *)(thread1.tasks->arg_list+0));
    //printf("%d\n",*(unsigned char *)(thread1.tasks->arg_list+1));
    //printf("%d\n",*(unsigned char *)(thread1.tasks->arg_list+2));   // this isn't working


    return 0;
}



Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7733
  • View blog
  • Posts: 25,851
  • Joined: 05-May 12

Re: malloc to pointer of void and assign different data types

Posted 28 March 2021 - 03:40 PM

Go back to your:
typedef void (*f_ptr)(void *args);



Then for each of your functions, you'll want to implement them as something like this:
void function1(void * args)
{
    printf("function1\n");
}

void function2(void * args)
{
    uint8_t i = (uint8_t) args;
    printf("function2# %d\n", i);
}

typedef
{
    uint8_t a;
    uint8_t b;
    uint8_t * img;
} MyStruct;

void function3(void * args)
{
    MyStruct * pStruct = (MyStruct *) args;
    printf("a#%d b#%d a#0x%.2x\n", pStruct->a, pStruct->b, pStruct->img[0]);
}

void functionThatTakesAnArray(void * args)
{
    int * intArray = (int *) args;
    for(int i = 0; i < 5; i++)        // assuming that the array have 5 elements
        printf("%d ", intArray[i]);
}


Was This Post Helpful? 1
  • +
  • -

#12 wolfrose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 20-January 16

Re: malloc to pointer of void and assign different data types

Posted 31 March 2021 - 10:12 AM

I was searching for a work around. I got back to copy a method of casting a function pointer.

And changed my data structure of having 2 nested structs to one that contain everything which should serve my project well.

Here's my work:


task_manager.h
////////////////////////////////////////////////////////////////////////////////////////
// enumerations
typedef enum {NOT_FINISHED,FINISHED,DONE}STATE;
// variables
typedef void (*f_ptr)(void *args);

// structs
typedef struct __attribute__((__packed__)) THREAD{
	uint8_t tsk_cnts, tsk_cntr;     // task information
	STATE   thrd_st;                  // thread flag states
    f_ptr   *tsk_fptr;
}THREAD;




main.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "task_manager.h"

const unsigned char PIC[] = {9, 8, 7, 1, 2};

void function1(void){printf("function1\n");}
void function2(uint8_t i){printf("function2\nnumber = %d\n",i);}
void function3(uint8_t a, uint8_t b, const uint8_t *img){
    printf("function3\na#%d b#%d\narray[0] = %d\n",a,b,*(img+0));}
uint8_t function4(uint8_t i){printf("function4# return\n"); return i+=1;}

THREAD thread1;

uint8_t fn4_ret;

int main(){

    // set thread main info
    thread1.thrd_st = 0;
    thread1.tsk_cnts = 4;
    thread1.tsk_cntr = 0;

    // allocating memory for tasks
    thread1.tsk_fptr = malloc(4*sizeof(f_ptr));

    // store tasks addresses
    thread1.tsk_fptr[0] = (void(*)())function1;
    thread1.tsk_fptr[1] = (void(*)())function2;
    thread1.tsk_fptr[2] = (void(*)())function3;
    thread1.tsk_fptr[3] = (void(*)())function4;

    // call the functions
    ((void(*)())thread1.tsk_fptr[1])(9);
    ((void(*)())thread1.tsk_fptr[2])(1,2,PIC);
    fn4_ret = ((uint8_t(*)())thread1.tsk_fptr[3])(1);
    printf("\fn4_ret = %d\n",fn4_ret);


    return 0;
}



It compiled with me fine, passed what I want and returned the required data.
Was This Post Helpful? 0
  • +
  • -

#13 wolfrose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 20-January 16

Re: malloc to pointer of void and assign different data types

Posted 03 April 2021 - 01:27 PM

Hi,

I've done this so far.

I think it's a lot of work casting everything. Do you agree with me ?

Is there a more simpler and more clearer way of doing this ?

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "task_manager.h"

THREAD thrd2;

const unsigned char PIC1[] = {3, 8, 7, 1, 2};

int main(){

    ///////////////////////////////////////////////////////////////////////////
    // thread config
    thrd2.thrd_st = NOT_FINISHED;
    thrd2.tsk_cnts = 5;
    thrd2.tsk_cntr = 0;

    // tasks memory allocation
    thrd2.tsk = malloc(5*sizeof(TASK));

    ///////////////////////////////////////////////////////////////////////////
    // #0: task0 setting    --    no args
    thrd2.tsk[0].tsk_fptr = (void(*)())function0;
    thrd2.tsk[0].args_cnts = 1;
    thrd2.tsk[0].tsk_st = 0;
    thrd2.tsk[0].tsk_args = NULL;

    ///////////////////////////////////////////////////////////////////////////
    // #1: task1 setting    --    1 arg
    thrd2.tsk[1].tsk_fptr = (void(*)())function1;
    thrd2.tsk[1].args_cnts = 0;
    thrd2.tsk[1].tsk_st = 0;

    thrd2.tsk[1].tsk_args = malloc(1*sizeof(uint8_t));
    *((uint8_t *)thrd2.tsk[1].tsk_args+0) = (uint8_t)9;


    ///////////////////////////////////////////////////////////////////////////
    // #2: task2 setting    --    3 args
    thrd2.tsk[2].tsk_fptr = (void(*)())function2;
    thrd2.tsk[2].args_cnts = 1;
    thrd2.tsk[2].tsk_st = 0;

    thrd2.tsk[2].tsk_args = malloc(2*sizeof(uint8_t)+1*sizeof(unsigned char*));

    *((uint8_t *)thrd2.tsk[2].tsk_args+0)  = (uint8_t)9;
    *((uint8_t *)thrd2.tsk[2].tsk_args+1)  = (uint8_t)3;
    *((uint8_t **)thrd2.tsk[2].tsk_args+2) = (unsigned char*)&PIC1;


    ///////////////////////////////////////////////////////////////////////////
    // #3: task3 setting    --    1 arg 1 return
    thrd2.tsk[3].tsk_fptr = (void(*)())function3;
    thrd2.tsk[3].args_cnts = 1;
    thrd2.tsk[3].tsk_st = 0;

    thrd2.tsk[3].tsk_args = malloc(2*sizeof(uint8_t)+
                                   1*sizeof(unsigned char*));
    *((uint8_t *)thrd2.tsk[3].tsk_args+0) = (uint8_t)15;

    ///////////////////////////////////////////////////////////////////////////
    // calling functions

    // task#0: 0 args
    ((void(*)())thrd2.tsk[0].tsk_fptr)(0);

    // task#1: 1 args
    ((void(*)())thrd2.tsk[1].tsk_fptr)(1);

    // task#2: 3 args
    ((void(*)())thrd2.tsk[2].tsk_fptr)(*((uint8_t *)thrd2.tsk[2].tsk_args+0),
                                       *((uint8_t *)thrd2.tsk[2].tsk_args+1),
                                       *((uint8_t **)thrd2.tsk[2].tsk_args+2));

    // task#3: 1 arg 1 ret
    *((uint8_t *)thrd2.tsk[3].tsk_args+1) =
    ((uint8_t(*)())thrd2.tsk[3].tsk_fptr)(*((uint8_t *)thrd2.tsk[3].tsk_args+0));

    printf("\n................................\nret_val = %d\n",
           *((uint8_t *)thrd2.tsk[3].tsk_args+1));

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7733
  • View blog
  • Posts: 25,851
  • Joined: 05-May 12

Re: malloc to pointer of void and assign different data types

Posted 03 April 2021 - 03:42 PM

You have all those casts because you insisted on allocating the space for the arguments (and return values) as void * and didn't want to cast the point into a usable structure or other type.

This:
thrd2.tsk[1].tsk_args = malloc(1*sizeof(uint8_t));
*((uint8_t *)thrd2.tsk[1].tsk_args+0) = (uint8_t)9;


would be so much more readable as:
uint8_t * arr = malloc(1*sizeof(uint8_t));
arr[0] = 9;
thrd2.tsk[1].tsk_args = arr;



or

thrd2.tsk[2].tsk_args = malloc(2*sizeof(uint8_t)+1*sizeof(unsigned char*));

*((uint8_t *)thrd2.tsk[2].tsk_args+0)  = (uint8_t)9;
*((uint8_t *)thrd2.tsk[2].tsk_args+1)  = (uint8_t)3;
*((uint8_t **)thrd2.tsk[2].tsk_args+2) = (unsigned char*)&PIC1;


written as:
typedef struct
{
    uint8_t a;
    uint8_t b;
    unsigned char * pic;
} MyParams;
:
MyParams * myparams = malloc(sizeof(MyParams));
myparams->a = 9;
myparams->b = 3;
myparams->pic = &PIC1;
thrd2.tsk[2].tsk_args = myparams;



As a quick aside, your calls to your functions on lines 61-77 are not making the calls that you think you are doing.
Was This Post Helpful? 1
  • +
  • -

#15 wolfrose   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 20-January 16

Re: malloc to pointer of void and assign different data types

Posted 04 April 2021 - 04:52 AM

View PostSkydiver, on 03 April 2021 - 03:42 PM, said:

You have all those casts because you insisted on allocating the space for the arguments (and return values) as void * and didn't want to cast the point into a usable structure or other type.

This:
thrd2.tsk[1].tsk_args = malloc(1*sizeof(uint8_t));
*((uint8_t *)thrd2.tsk[1].tsk_args+0) = (uint8_t)9;


would be so much more readable as:
uint8_t * arr = malloc(1*sizeof(uint8_t));
arr[0] = 9;
thrd2.tsk[1].tsk_args = arr;



What is the 2nd snippet exactly, didn't get it.

It's a malloc to a pointer to uint8_t, so it's a size of an address.

But you put two values 9 and arr, I didn't understand this example.



Quote

thrd2.tsk[2].tsk_args = malloc(2*sizeof(uint8_t)+1*sizeof(unsigned char*));

*((uint8_t *)thrd2.tsk[2].tsk_args+0)  = (uint8_t)9;
*((uint8_t *)thrd2.tsk[2].tsk_args+1)  = (uint8_t)3;
*((uint8_t **)thrd2.tsk[2].tsk_args+2) = (unsigned char*)&PIC1;



written as:

typedef struct
{
    uint8_t a;
    uint8_t b;
    unsigned char * pic;
} MyParams;
:
MyParams * myparams = malloc(sizeof(MyParams));
myparams->a = 9;
myparams->b = 3;
myparams->pic = &PIC1;
thrd2.tsk[2].tsk_args = myparams;



But what if I have many functions with different argument sets.

void glcd_init(void);
void glcd_cmd(uint8_t cmd);
void glcd_data(uint8_t data);
void glcd_manager(void);
void glcd_delay_routine(void);
void glcd_graphics_mode(bool mode);
void glcd_move_cursor(uint8_t row, uint8_t col);
void glcd_clr(void);
// draw functions
void glcd_img(const uint8_t *img);
void glcd_pixel_set(uint8_t x_axis, uint8_t y_axis);
void glcd_pixel_clear(uint8_t x_axis, uint8_t y_axis);
void glcd_line(uint8_t y0, uint8_t y1, uint8_t x0, uint8_t x1);
void glcd_circle(uint8_t y, uint8_t x, uint8_t r);
// online functions
void circle(int xc, int yc, int radius);
void midPointCircleDraw(int y, int x, int r);
void circleBres(int xc, int yc, int r);
void drawCircle(int xc, int yc, int x, int y);
// development
void circle_sketch(uint8_t x0, uint8_t y0, uint8_t radius);



Now what to do here ?

Also I think it would be so difficult to use one function pointer to callback all these types of functions and pass their required arguments.

one told me to use this:

typedef void (*f_ptr)();



Which has an empty argument, but when I callback the function, how to pass its related arguments ? This is one of my very important concerns about doing a multithread manager.

Quote

As a quick aside, your calls to your functions on lines 61-77 are not making the calls that you think you are doing.


But they are working ! and worked on all the warnings and bugs until got it to work.
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »