c - 当我注释掉 printf 语句时,为什么会出现段错误

标签 c

如果我注释掉 dicksonsmethod 函数中的 printf 行,则会出现段错误。如果我把它留在代码中运行并产生正确的答案。为什么? 我已经包含了所有代码,因为可能只有一部分代码

#include<stdio.h> 
#include <time.h>
#include <math.h>

int bruteforce(int max){
}
int bruteforcerefined(int max){
}

int dicksonsmethod(int max){
printf("The Dickson method\n");// If I comment out this line I get a Segmentation fault.
int r,dm,fp;
int a=1,b=2,c=3;
int perimeter,multiplier;
int paircount=0;
int factorpairs[2][20];

for (r=2;;r+=2){ // infinite loop
     dm=r*r/2;

     paircount=0;// Get the count of factor pairs, and the pairs of factors
     for (fp=1;fp<(dm/2)+1;fp++){
         if (dm%fp==0){
             if(fp >=dm/fp){ // fp >=dm/fp to avoid duplicating pairs in reverse
                 break; // do not want to get reverse facor pairs
            }
            paircount+=1;
            factorpairs[paircount][0]=fp;
            factorpairs[paircount][1]=dm/fp; 
        }
    }
    for (fp=1;fp<=paircount;fp++){  // for each Dickenson pair
        a=r+factorpairs[fp][0]; // get the pythagorean triplet
        b=r+factorpairs[fp][1];
        c=r+factorpairs[fp][0]+factorpairs[fp][1];
        perimeter=a+b+c;
        if(max%perimeter==0){ // and see if it will scale to required size
            multiplier=(int)max/perimeter;
            //          printf("Sides %d %d %d as a triplet, the sum being %d, from factor pairs %d and %d \n",a,b,c,perimeter=a+b+c,factorpairs[fp][0],factorpairs[fp][1]);
            return a*b*c*multiplier*multiplier*multiplier;
        }
    }
}
}

int main()
{
int max=1000;
char str;
while (1){
printf("Compare various methods of finding Pythagorean Triples to fullfil the specified conditions\n");
printf("1 - The Brute Force method.\n");
printf("2 - The Brute Force method, refined.\n");
printf("3 - The Dickson\'s method.\n");
printf("The Fibbonacci method, does not product usable triplets in a timely manner.\n");
printf("The Squared Difference, special method, does not product usable triplets in a timely manner.\n");
printf("The Squared Difference, general method, does not product usable triplets in a timely manner.\n");
printf("Enter a number 1 - 3 to execute or E to exit:- ");
scanf(" %c",&str);
printf("\n\n\n");
clock_t begin, end;
double time_spent;
begin = clock();
/* here, do your time-consuming job */
if (str == '1') {
    printf("Brute force produces %d\n",bruteforce(max));
}
if (str == '2') {
    printf("Checking multiples of triplets produces %d\n",bruteforcerefined(max));
}
if (str == '3') {
    printf("The Dickson\'s method produces %d\n",dicksonsmethod(max));
}
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf(" and took %f seconds",time_spent);
if(str == 'E' || str == 'e'){
    return 1;
}printf("\n\n\n");
}
}

最佳答案

您正在破坏堆栈,从而导致崩溃。我不会详细介绍您的算法,但明显的事情是您的 factorpairs 数组被写入越界。观察 paircount 为我生成的快速调试:

Old value = 1
New value = 2
dicksonsmethod (max=1000) at a.c:32
32          factorpairs[paircount][0] = fp;

即你的函数在某个时刻试图写入越界,因为paircount已经变成2了。

它与 printf 一起运行的原因是因为您处于未定义的行为领域。

关于c - 当我注释掉 printf 语句时,为什么会出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29357896/

相关文章:

c - 当我将它与 protobuf 生成的文件链接时,openssl 中的程序核心

c++ - 从C迁移到C++

c - 为什么标准提到分配存储中的分配顺序

c - recvfrom() 套接字编程 Ubuntu

c - 打印结构数组

c - 如何从 ELF 文件中提取通过编译器优化添加的常量地址?

c - 使用 C 中的 Expat 库解析 XML 文件

C - 如何存储 XML 字符串并保持格式?

c - 如何仅使用指针在 C 中重新调用字符串数组

c - 我们如何从命令行构建一个 visual studio 项目