[英]Getting segmentation fault for the code below. I am not able to figure out where am I going wrong
Here is the code that will implement the formula (1+x)^2: 这是将实现公式(1 + x)^ 2的代码:
#include <stdio.h>
#include <stdlib.h>
#include "nCr.h"
#include <time.h>
#include <sys/time.h>
int main(int argc, const char * argv[])
{
int k = 0;
int n;
int c;
struct timeval start, end;
if (argv[1][0] == '-' && argv[1][1] == 'h') {
printf("Usage: formula <positive integer>");
} else {
n = atoi(argv[1]);
// gettimeofday will give the execution time of program in microsecond.
gettimeofday(&start, NULL);
printf("(1 + x)^%i = ", n);
if (n == 0)
printf("0");
for (; k <= n; k++) {
// Here nCr is an assembly code which compute coefficient
c = nCr(n, k);
if (c == -1) {
printf("Multiplication overflow. \n");
return 1;
} else {
if (k != 0)
printf("%i x^%i ",c , k);
if (k != n && k != 0)
printf("+ ");
}
}
gettimeofday(&end, NULL);
}
printf("\n%ld microseconds\n", ((end.tv_sec * 1000000 + end.tv_usec)
- (start.tv_sec * 1000000 + start.tv_usec)));
return 0;
}
getting segmentation fault on Linux gcc 在Linux gcc上出现分段错误
It may happen because you try to access arguments that doesn't exist. 可能是因为您尝试访问不存在的参数而发生。 Before accessing arguments add argc check.
在访问参数之前,请添加argc检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.