I was playing around with many different and stupid forms of loops when I came to an idea of loop that I temporary called FIF loop (function loop).
It works pretty well (it's 10 times slower than regular loop but nvm that for now) till it does exactly 174665 repetitions. On 174665th repetition it throws Cannot access memory at address
on *k pointer in line:
void fif(bool (*f)(int *x),int i,int *k){
. It always crashes in the same point (same repetition). Any ideas why? Testing on Ubuntu 15.10, gcc version 5.2.1 20151010. I'm new to C so please, be patient to newbi :). Thanks in advance for any help
My code:
#include <stdio.h>
#include <stdbool.h>
#define REPEATS 1.8E5
#ifdef WIN32
#include <windows.h>
double get_time()
{
LARGE_INTEGER t, f;
QueryPerformanceCounter(&t);
QueryPerformanceFrequency(&f);
return (double)t.QuadPart/(double)f.QuadPart;
}
#else
#include <sys/time.h>
#include <sys/resource.h>
double get_time()
{
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
return t.tv_sec + t.tv_usec*1e-6;
}
#endif
bool fifFunction(int *k);
void fif(bool (*f)(int *x),int i,int *k);
int main()
{
//FIF
printf("FIF loop\n");
double t = get_time();
int k = 0;
fif(fifFunction,REPEATS,&k);
printf("time: %f\n",get_time() - t);
return 0;
}
bool fifFunction(int *k)
{
return (*k = *k + 1);
}
void fif(bool (*f)(int *x),int i,int *k){
if (i > 0){
if ((*f)((k)) == false){
return;
}
fif(f,(i-1),k);
}
}
It's because you blow out the call stack.
void fif(bool (*f)(int *x),int i,int *k){
if (i > 0){
if ((*f)((k)) == false){
return;
}
fif(f,(i-1),k); // HERE
}
}
On the line marked HERE
, you recurse, and push the variables x
, i
, and k
onto the stack. After enough times, you run out of space and the program crashes. If you compile with -O3
, gcc will convert this to iteration since it's a tail-recursive call, but you should absolutely not rely on this behavior. You should instead write this with a loop.