C function call dynamically


 
Thread Tools Search this Thread
Top Forums Programming C function call dynamically
# 8  
Old 12-10-2009
Quote:
Originally Posted by DreamWarrior
I think the short answer is no. As far as I know, there is no way in C to dynamically control the call stack.
I've seen it done, like you say, in inline ASM. Very hairy ASM that's specific to carefully controlled circumstances under one configuration of one compiler of one version of one architecture. I tried building one myself, and quickly gave up when I spotted the optimizer stuffing arguments into registers instead of stack whenever it felt like it.

It should really be up to the one thing that really knows how function calls work at all times -- the compiler, plus macros in stdargs. It's one of the few severely un-orthogonal things left in C but there's obscure reasons this isn't possible on a few strange architectures, ergo it's not going to happen.
# 9  
Old 12-10-2009
Just as a note, I did some quick tinkering and it appears the stack frame for Linux (probably ia32 in general) stores the parameters in the stack frame of the callee. So, in contrast to AIX, coding your generic thrower in Linux will require you to allocate a brand new stack frame, put all the arguments in it, configure it correctly to call the procedure, and fire it off. Non-trivial at best.

Having no experience with the reflection library, I don't know if it can be used to build dynamic calls. If so, great.... But, I have my doubts.
# 10  
Old 12-10-2009
pogdorica - You want an array of function pointers.

Code:
/* function pointers - std C allows ([blank])== any number/datatypes of parms  */
#include <stdlib.h>
#include <stdio.h>
void foo1(int p)
{
   printf("function foo1:%d\n", p);
}

void foo2(char *src, const int p)
{
   sprintf(src, "function foo2: %d", p);
   printf("%s\n", src);
}

void foo3(const int a, const int b, const int c)
{
	  int total=a + b + c;
	  printf("function: foo3(): %d + %d + %d = %d\n", a, b, c,  total);
}

typedef void (*ptr2Func)();

int main(int argc, char **argv)
{
   ptr2Func  fx[3]={foo1, foo2, foo3};
   char string[32]={0x0};
   fx[0](9);
   fx[1](string, 4);
   fx[2](1,2,3);
   return 0;
}

Code:
# compile with zero errors & warnings
> gcc -Wall -pedantic fptr.c    

>

run it:
Code:
> ./a.out                 
function foo1:9            
function foo2: 4               
function: foo3(): 1 + 2 + 3 = 6

# 11  
Old 12-10-2009
Quote:
Originally Posted by DreamWarrior
Just as a note, I did some quick tinkering and it appears the stack frame for Linux (probably ia32 in general) stores the parameters in the stack frame of the callee.
Only unoptimized. Optimization can cause unpredictable things -- I've seen gcc do weird things like stuff floating-point doubles into SSE registers when calling fprintf.
# 12  
Old 12-10-2009
Dream Warrior - consider trampolines or indirect function calls found most often in callbacks. I think python uses trampolines to support coroutines. ghostdog would know for sure.

Linux trampoline code is described in great detail in: 'Understanding the Linux Kernel' by Daniel P. Bovet and Marco Cesati.

gcc documentation:
Trampolines - GNU Compiler Collection (GCC) Internals



The lazy man's version -
A trampoline is small code segment, usually a vector, created at run time. It normally lives on the stack, in the stack frame of the 'holding' function.

For mixing pieces of code with incompatible calling conventions, trampolines can convert the caller's convention to the callee's convention. For example, C code that needs to call C++ objects can have a trampoline to change calling conventions.

gcc implements trampolines as nested functions.
# 13  
Old 12-10-2009
Quote:
Originally Posted by Corona688
Only unoptimized. Optimization can cause unpredictable things -- I've seen gcc do weird things like stuff floating-point doubles into SSE registers when calling fprintf.
Odd...how would fprintf know where to pick them up if not from the stack....?

---------- Post updated at 03:57 PM ---------- Previous update was at 03:52 PM ----------

Quote:
Originally Posted by jim mcnamara
Dream Warrior - consider trampolines or indirect function calls found most often in callbacks. I think python uses trampolines to support coroutines. ghostdog would know for sure.

Linux trampoline code is described in great detail in: 'Understanding the Linux Kernel' by Daniel P. Bovet and Marco Cesati.

gcc documentation:
Trampolines - GNU Compiler Collection (GCC) Internals



The lazy man's version -
A trampoline is small code segment, usually a vector, created at run time. It normally lives on the stack, in the stack frame of the 'holding' function.

For mixing pieces of code with incompatible calling conventions, trampolines can convert the caller's convention to the callee's convention. For example, C code that needs to call C++ objects can have a trampoline to change calling conventions.

gcc implements trampolines as nested functions.
I'm unsure how this could be used to dynamically figure out the arguments of another function and populate the call stack and call it.... And the man page you posted is above my head Smilie.
# 14  
Old 12-11-2009
Thank you very much for your suggestions. As i suspected that task is so complicated really.
I 'll try to use an array of function pointers with an only parameter with generic type that it is a list with the type and value of each parameter to the function.
Something such as:

Code:
// Struct with parameter info
typedef struct {
        char nombreParametro[30];
        void *valor;
} s_parametro;
typedef ListaParametros List<s_parametro>;

// Struct with function info
typedef struct {
        char nombreFuncion[30];
        void (*ejecutame)(ListaParametros *);
} s_funcion;

So, i 'll say you any advance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

How to Dynamically Pass Parameter to plsql Function & Capture its Output Value in a Shell Variable?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: #! /bin/ksh v="ORG_ID" ... (2 Replies)
Discussion started by: sujitdas2104
2 Replies

2. Shell Programming and Scripting

How to dynamically name as function in shell?

Hi all, Does anyone know if it possible to append a parameter to a function name? Something like the following: function tnsrec_${SERVICE_NAME} { code.. } Any ideas? (6 Replies)
Discussion started by: jonnyd
6 Replies

3. Shell Programming and Scripting

After exit from function it should not call other function

Below is my script that is function properly per my conditions but I am facing one problem here that is when one function fails then Iy should not check other functions but it calls the other function too So anyone can help me how could i achieve this? iNOUT i AM GIVING TO THE... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

4. Shell Programming and Scripting

Function call

Hi foiks i am unable to find what is wrong in my code mu functionality is to exit from shell when i give 99 but it is not calling function ext Could you please correct me. read option if ; then ext else echo "out" fi function ext { echo "tested 99 and exit... (12 Replies)
Discussion started by: kojo
12 Replies

5. Infrastructure Monitoring

diffrence between method call and function call in perl

Hello, I have a problem with package and name space. require "/Mehran/DSGateEngineLib/general.pl"; use strict; sub System_Status_Main_Service_Status_Intrusion_Prevention { my %idpstatus; my @result; &General_ReadHash("/var/dsg/idp/settings",\%idpstatus); #print... (4 Replies)
Discussion started by: Zaxon
4 Replies

6. Shell Programming and Scripting

Function Call

How we can start a process if doesn't exists before? (1 Reply)
Discussion started by: Zaxon
1 Replies

7. Shell Programming and Scripting

help on function call

hello, when i call function inside awk traitement it doesn't work, i don't have error execution but i don't get result and if i call the function outside awk traitement it work well.. there's something special in awk call function?? here is the example : awk -F "," '{ {first=$1; sec=$2;... (3 Replies)
Discussion started by: kamel.seg
3 Replies

8. Shell Programming and Scripting

call function

I have a function check_ok in my abc.sh. which return me 1 or 0 . I want to call this fuction through other shell script. this shell also send two parameter to calling function. Can you please tell me how. I am very new in unix. #!/bin/bash date_equal() { sqlplus -silent... (4 Replies)
Discussion started by: Jamil Qadir
4 Replies

9. Programming

Help with a function call

Hi, Can anyone help me figure out the problem I'm having with a function call? I have a header file, which sets an enum: typedef enum {INFO, WARNING, FATAL} Levels; int log_event (Levels, char *fmt, ...); ..then the function is called this way: log_event(INFO, "Message text"); ... (6 Replies)
Discussion started by: Stevhp
6 Replies

10. Shell Programming and Scripting

function call

can I call a function in bash script just as in C++ while do function() done function() thanks, Steffen (3 Replies)
Discussion started by: forever_49ers
3 Replies
Login or Register to Ask a Question