Intialize the list of functions with runtime library common function 
Author Message
 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  
 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:

>y.c
>---

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  
 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++;
  }

- Show quoted text -

Quote:
}



Sun, 25 Sep 2005 04:10:14 GMT  
 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

- Show quoted text -

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  
 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  
 
 [ 5 post ] 

 Relevant Pages 

1. a C library for runtime function constructing

2. A Thread class that supports MFC as well as C/C++ runtime-library functions

3. DLL function calling problems (explicit - only knowing function name at runtime)

4. Listing of C library functions

5. NEEDED: portable dir listing function/library

6. STILL NEEDED: *online* list of ANSI C library functions

7. NEEDED: list of ANSI C library functions

8. A definitive list of common libraries

9. C function calling function in C++ library

10. Calling DLL functions without known function name, parameter-list until run-time

11. Help needed to move some specific work into a more common function

12. Function Pointer Common Usage

 

 
Powered by phpBB® Forum Software