我编写了一个递归函数,并将指向整数的指针作为参数传递。该整数值在函数中递增,但我面临一个奇怪的问题,即在某个值之后它的值永远不会更新。即使我正在检查该地址的值。
以下是代码: -
computeWait(long long int begin, long long int begin2, long long int w,
int* current, int limit)
{
long long int next = 0, arrival = 0;
long long int next1 = 0, service = 0;
long long int serviceTime = 0;
long long int wait = 0;
static long long int Ta = 0;
static long long int Ts = 0;
static long long int W = 0;
while(*current < limit)
{
next = (16807 * begin) % m;
arrival = -200 * log((double)next/m);
next1 = (16807 * begin2) % m;
service = -100 * log(EDRN((double)next1));
wait = max(0, (w + service - arrival));
Ta = Ta + arrival;
Ts = Ts + service;
W = W + wait;
*current = *current + 1;;
computeWait(next, next1, wait, current, limit);
}
printf("\n\nTotal arrival %Ld Total service %Ld Total wait %Ld\n", Ta/limit, Ts/limit, W/limit);
}
int main(int agrc, char* argv[])
{
int num = 0;
int currentValue = 0; // seed number
int end = 1000000;
computeWait(1, 46831694, 0, ¤tValue, end);
}
在103917之后,它的值不会更新,它会导致内存保护失败。
请让我知道我做错了什么,因为修复它似乎很简单。
谢谢, NEHA。
答案 0 :(得分:3)
好吧,我并没有真正尝试理解代码,但是我看到了一些大数字和递归这个词。 所以我猜它堆栈溢出因为你的递归太深了?