Back | Reverse | Quick Reply | Post Reply |

C++ and (possible) assembly help
Link | by Children of Lilith on 2008-05-08 12:32:36 (edited 2008-05-08 12:34:47)
I'm still kinda a noob at C++ programming. However, I know that when a program is compiled, when you call a function the program jumps to wherever that function is stored in memory (I think). Does this mean that I can access from within my program the memory address of the beginning of the function? The reason I want to do this is because I'm programming a program that will have the following requirements:

The ability to call several different pre-defined functions in a user defined order that will be defined at run-time.
The number of possible functions will be very big
This variable-function-execution process will be happening a good deal number of times.

The following restrictions make me very resistant to the idea of doing the following:
  • Assign an arbitrary integer value that will signify a different function
  • Have the user enter in the desired functions in their desired order
  • Store the order and the identifier of the functions into an array of i length, where i is the number of functions that the program will be calling
  • doing the following: for(int n; n < i;n++) { switch (instructionArray[n]){ case 0: whateverFunction0Is(); case 1: whateverFunction1Is(); [etc....] }

Basically, can I have a variable that stores a function, not a value?

Any suggestions? If the above scenario makes no sense, I'll try and clarify what I'm asking for.

Thanks in advance

Re: C++ and (possible) assembly help
Link | by gendou on 2008-05-08 12:53:48
this sounds like a basic queued-command programming model, and should be very easy to setup.
my suggestion is, give it a try and ask a specific question if you get stuck.
if you don't know where to begin, start with main.
your main function will be a loop, prompting for i commands, and queuing them up.
then, once the queue is full, it will execute them in order.


Re: C++ and (possible) assembly help
Link | by Donuts on 2008-05-16 04:48:33
You can have pointers to functions, but each function pointer must be defined with the same arguments and return type as the function it points to, meaning if you want to execute a bunch of functions you'll probably want them all to take the same args and return type.

//Pointer to main
int (*ptr_main)(int, char**);
ptr_main = &main;

Back | Reverse | Quick Reply | Post Reply |

Copyright 2000-2024 Gendou | Terms of Use | Page loaded in 0.0152 seconds at 2024-12-28 21:47:20