Intialize the list of functions with runtime library common function
Author |
Message |
Avi #1 / 5
|
 Intialize the list of functions with runtime library common function
Hi All, I have init function in two different file x.c & y.c, like follow: x.c --- init_x() { ...my code.. } y.c --- init_y() { ...my code.. } I want to initialize 2 above function with runtime lib function init_all(). "init_all()" should be a common function to intialize all of the above functions: main.c ------ #include "rtLib.h" init_all(); Is there any way to add(init_x(), init_y()) into list or linklist of functions & intialize all of the above functions using init_all() call. rtLib.h ------- extern init_all(); rtLib.c ------- init_all() { //I want initialize init_x(), init_y() functions over here //Please note: I can't write init_x(), init_y() over here, as //init_all() is in runtime lib, written before x.c, y.c file //But I can define a linklist of function & call //each of the functions in list over here Quote: }
Any Idea.. Thank you for your time. Avin Patel
|
Sat, 24 Sep 2005 22:53:25 GMT |
|
 |
Dan P #2 / 5
|
 Intialize the list of functions with runtime library common function
Quote: >I have init function in two different file x.c & y.c, like follow: >x.c >---
void init_x() { ...my code.. } Quote: void init_y() { ...my code.. } Quote: >I want to initialize 2 above function with runtime lib function >init_all(). "init_all()" should be a common function to intialize all >of the above functions: >main.c >------ >#include "rtLib.h"
vf init_x, init_y; vf *initlist[] = {init_x, init_y, NULL}; Quote: >init_all(); >Is there any way to add(init_x(), init_y()) into list or linklist of >functions & intialize all of the above functions using init_all() >call. >rtLib.h >-------
extern void init_all(void); typedef void vf(void); extern vf *initlist[]; Quote: >rtLib.c >-------
#include "rtLib.h" void init_all() { Quote: >//I want initialize init_x(), init_y() functions over here >//Please note: I can't write init_x(), init_y() over here, as >//init_all() is in runtime lib, written before x.c, y.c file >//But I can define a linklist of function & call >//each of the functions in list over here
int i; for (i = 0; initlist[i] != NULL; i++) (*initlist[i])(); Quote: >}
Dan -- Dan Pop DESY Zeuthen, RZ group
|
Sun, 25 Sep 2005 00:23:59 GMT |
|
 |
Mi #3 / 5
|
 Intialize the list of functions with runtime library common function
Quote: > I have init function in two different file x.c & y.c, like follow: > x.c > --- > init_x() { ...my code.. } > y.c > --- > init_y() { ...my code.. } > I want to initialize 2 above function with runtime lib function > init_all(). "init_all()" should be a common function to intialize all > of the above functions: > main.c > ------ > #include "rtLib.h" > init_all();
First, if I understand you right, you're writing the code (or have access to the code) for init_all(). Assuming that to be true, you could just use an array of pointers (which in this case would hold pointers to functions). Pass the pointer to init_all() (or use a global pointer variable if you want to, depending on your preferences with respect to global namespace pollution). In init_all() go in a while loop and iterate for every pointer you find in the array and stop when you find a null pointer. Something like the following should work (this is a rough sketch:check for errors and/or bad coding) typedef void (*f)(); /* declare pointer to function type */ void init_x() { printf("init_x called\n"); Quote: }
void init_y() { printf("init_y called\n"); Quote: }
void init_z() { printf("init_z called\n"); Quote: }
f fp[4] = {init_x,init_y,init_z,0}; /* terminate array with null to signify end */ --- somewhere else in rtlib.c --- init_all() { int i = 0; while (fp[i] != 0) { fp[i](); /* call function */ i++; } Quote: }
|
Sun, 25 Sep 2005 04:10:14 GMT |
|
 |
Avi #4 / 5
|
 Intialize the list of functions with runtime library common function
Quote:
> > I have init function in two different file x.c & y.c, like follow: > > x.c > > --- > > init_x() { ...my code.. } > > y.c > > --- > > init_y() { ...my code.. } > > I want to initialize 2 above function with runtime lib function > > init_all(). "init_all()" should be a common function to intialize all > > of the above functions: > > main.c > > ------ > > #include "rtLib.h" > > init_all(); > First, if I understand you right, you're writing the code (or have > access to the code) for init_all(). Assuming that to be true, you > could just use an array of pointers (which in this case would hold > pointers to functions). Pass the pointer to init_all() (or use a > global pointer variable if you want to, depending on your preferences > with respect to global namespace pollution). In init_all() go in a > while loop and iterate for every pointer you find in the array and > stop when you find a null pointer. > Something like the following should work (this is a rough sketch:check > for errors and/or bad coding) > typedef void (*f)(); /* declare pointer to function type */ > void init_x() > { > printf("init_x called\n"); > } > void init_y() > { > printf("init_y called\n"); > } > void init_z() > { > printf("init_z called\n"); > } > f fp[4] = {init_x,init_y,init_z,0}; /* terminate array with null to > signify end */
This solution will not work, As init_x() is in x.c, init_y() is in y.c & init_z() is in z.c. I know you suggested solution, but where can I write "f fp[4] = {init_x,init_y,init_z,0};". As I have mentioned, init_all() is in runtime library. My problem is I have to add init_x() function in function pointer list/array in x.c(same file of init_() function) & same procedure for init_y(), init_z(). And one more thing, no. of init function are not same all time. I need a solution, whenever I write init_xyz() function & I should be able to add that function in initilization array() in same .c file. There is no other file, where I can write things like follow: f fp[4] = {init_x,init_y,init_z,0}; //As no files know how many init() I have Thank you for your replies & time. Avin Patel Quote: > --- somewhere else in rtlib.c --- > init_all() > { > int i = 0; > while (fp[i] != 0) > { > fp[i](); /* call function */ > i++; > } > }
|
Thu, 29 Sep 2005 21:38:45 GMT |
|
 |
vasua #5 / 5
|
 Intialize the list of functions with runtime library common function
Avin, It seems that you have a problem that you need to pass a data-structure to a function, which could be a global data-structure, but you don't have a place where you want to initialize this. That is the key to your problem and I guess you will have to figure out a way to initialize this global data-structure, in the application (main()) or somewhere else. There is no way that the init_all() can figure out on it's own where to look if it is a runtime library. Take a look at the other implementations out there, especially in the provided runtime libraries, and see what they do for the same thing. Sorry for not having a simpler solution for you. av.
|
Fri, 30 Sep 2005 05:19:00 GMT |
|
|
|