I know it may sound sci-fi but I truly want to call a function x times, by using an array of function pointers to it and without involving a loop or anything that may slow down the target program. Is that possible and if yes, how exactly?
-
6It is certainly possible, but why would you need an array of function pointers to call a function x times? Just call it x times. – Wintermute Mar 04 '15 at 22:07
-
Not withstanding what @Wintermute said, you would declare an array of the desired size and then copy the same function pointer to each array element. That wastes RAM and almost certainly slower than just calling the pointer in a loop. – Eric J. Mar 04 '15 at 22:09
-
Create a a temp variable to point to the beginning of the array and of the type of pointer inside the array. Then use the temp variable to move through the array counting the number of pointers. – pmac89 Mar 04 '15 at 22:09
-
I am creating something like threading. I tested that running function x times instead of looping it, is faster. – DeltaProxy Mar 04 '15 at 22:10
-
6Your edit sounds like a micro-optimization that is very unlikely to matter in most real-world scenarios (on a modern CPU at least). – Eric J. Mar 04 '15 at 22:10
-
You are right. But because of an arcane reason, lots of people still work on ancient environments. – DeltaProxy Mar 04 '15 at 22:14
-
Then why not create a loop and inside the loop push the function to a thread? – pmac89 Mar 04 '15 at 22:14
-
1If you want to call the function some number of times that is not known at compile time, then you probably cannot avoid overhead comparable to that of a loop. And that overhead is surely tiny compared to the run time of the function itself. If you want to call it a fixed number of times, then just do that. – John Bollinger Mar 04 '15 at 22:14
-
In fact, if the number of times to invoke the function is determined dynamically, then recursion is the only alternative I see to a loop, and that will surely have much higher overhead. – John Bollinger Mar 04 '15 at 22:22
-
It is not about many functions looped, it is about many functions called at once. – DeltaProxy Mar 04 '15 at 22:24
-
3Making copies of function pointer does not make the function thread-safe. The function is still a function, and it's static storages are still in one place. If the function is already thread safe, then you don't need to do anything, just multi-thread it. Edit: google for SPMD or OpenCL – user3528438 Mar 04 '15 at 22:25
-
If I'm following the logic, there are no errors or something.. but by invoking the array of pointers, the function they have assigned to point to is called only once. I thought all the pointers will be called. – DeltaProxy Mar 04 '15 at 22:37
-
2Ok, now I'm totally confused. You started with *"running function x times instead of looping"*, then moved to *"many functions called at once"* and then to *"the function ... is called only once"*. I think you need to decide what the heck you're actually trying to do, and then edit the question to describe that. – user3386109 Mar 04 '15 at 22:41
-
What I want remains the same. I am just clarifying.. Because I was told to use recursion, where I clearly said without loops. I think I meant at once by that.. because loops do not call a function at once rather than one at a time. – DeltaProxy Mar 04 '15 at 22:43
-
Ok, now we're getting somewhere. Calling a function without a loop is not the same as recursion. Recursion has a very specific meaning, and if you want to call functions recursively, you need to put the word "recursion" somewhere in the question, so that people know what you're talking about. Also, calling a function recursively is just a deceitful way of looping it that uses a lot of stack space. It doesn't just call the function once, since the function calls itself. So, yes now I sort of see what you're getting at. – user3386109 Mar 04 '15 at 22:48
-
Oh erm, of course loop and recursion are two different things, besides that point, they run in a loop style (not at once, looped). No I am one hundred percent you know what I mean. By they way I know why http://hastebin.com/isotaruqaj.cpp prints out 1 as only one function is called. Because `foo();` refers to the first item only. – DeltaProxy Mar 04 '15 at 22:51
-
So the only thing left that needs to be clarified is the array of function pointers. What is the purpose of the array? Do you have different functions loaded in the array? Or do you only want to the call the same function x times (in which case you don't need an array)? – user3386109 Mar 04 '15 at 22:58
-
The second. I want to call one and the same function x times, without loops. At least just like I call it several times. The only way for this I can figure out is by calling them all at once, through an array of function pointers, like it is exemplified in the link I provided above, which buffer's items are initialized manually. – DeltaProxy Mar 04 '15 at 23:01
-
Seems like what you want is depth limited recursion. I'll post an example as an answer. – user3386109 Mar 04 '15 at 23:03
-
`void f(); int main() { f(); f(); f(); return 0; }` just like that but automatized with dynamic number of calls given. This doesn't involve loops as you see, but is with fixed number of calls given. – DeltaProxy Mar 04 '15 at 23:04
-
@DeltaProxy "just like that but automatized with dynamic number of calls" – that's called a loop. you can't call a function (or execute any other statement, for that matter) an indeterminate number of times without looping or recursion. – The Paramagnetic Croissant Mar 04 '15 at 23:09
-
This is the answer I was looking for. Is it possible or not.to achieve this without loops. I said it might sound insane.. but it would be good if I could call a function x times without loops. – DeltaProxy Mar 04 '15 at 23:10
-
3What is your obsession with loops? In all cases, the execution of the function **body** costs more (cpu time) than the **invocation** by the caller. Do you actually want *parallellism* ? (calling a function before the previous function call has returned) – wildplasser Mar 04 '15 at 23:15
2 Answers
Here's an example of depth limited recursion
void hello( int depth )
{
printf( "hello %2d\n", depth );
if ( depth > 1 )
hello( depth - 1 );
}
int main()
{
hello( 10 ); // call the hello function 10 times
hello( 17 ); // call the hello function 17 times
}
And here's an example of calling a function pointer multiple times using a jump table. Note that with the jump table, the maximum number of calls is limited to the size of the jump table (10 in this example) whereas with recursion, the number of calls is limited only by stack size.
int hello( int n )
{
n++;
printf( "hello %2d\n", n );
return n;
}
void executeFunction( int (*func)(int), int count )
{
int n = 0;
switch ( count )
{
case 10: n = (*func)(n);
case 9: n = (*func)(n);
case 8: n = (*func)(n);
case 7: n = (*func)(n);
case 6: n = (*func)(n);
case 5: n = (*func)(n);
case 4: n = (*func)(n);
case 3: n = (*func)(n);
case 2: n = (*func)(n);
case 1: n = (*func)(n);
}
}
int main()
{
executeFunction( hello, 3 ); // call the hello function 3 times
executeFunction( hello, 9 ); // call the hello function 9 times
}

- 34,287
- 7
- 49
- 68
-
....this will make the program come to a standstill for a long period of time, especially if the number of function calls is big. This is why I explicitly said.. WITHOUT LOOPS. A number of times. `for(; ;)` is the most obvious loop. – DeltaProxy Mar 04 '15 at 23:08
-
@DeltaProxy you obviously don't understand what the for loop is used for here. it's an "infinite" loop used **solely** for getting user input. The essence of this answer is the recursion. The loop could be left out entirely. – The Paramagnetic Croissant Mar 04 '15 at 23:11
-
-
-
@user3386109 unfortunately I can do this only with recursive functions. – DeltaProxy Mar 04 '15 at 23:16
-
You could use the pre-processor to expand a macro to x number of function calls. Your code will get larger and you'll still run the function x number of times. It won't win you anything.
#define REPEAT5(x) { x; x; x; x; x; }
Note that you can't call that macro like a function (it'll expand to a syntax error if you end the call with a semicolon). A cleaner macro (though in this context, what does it matter?) would be:
#define REPEAT5(x) do { x; x; x; x; x; } while(0)
Then you could call the macro like a function, but that expands to a loop and you said you don't like loops.