c - 返回零后出现段错误

标签 c segmentation-fault

本学期我们正在学习使用 C 进行编程,在我们的第一个作业中,我们被要求使用手动和库实现来打印 sin(x)、cos(x) 和 tan(x) 的值列表。因此,我编写了以下代码:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define START   0
#define STOP    360
#define STEP    10

#define PI      3.14159265358979323846  /*For conversion: degrees <-> radians */

double rad(double x);   /* Converts angle in degrees to radians */
double next_term(double angle, int term_index);

/* Function prototypes for manual implementation of sin(x), cos(x) and tan(x) */
double sin_series(double x);
double cos_series(double x);
double tan_series(double x);


int main() {

    /* Creating and printing the table title:
    ** ==================================================================== */

    char table_title[] = "   n | ";
    strcat(table_title, "sin series | sin library | ");
    strcat(table_title, "cos series | cos library | ");
    strcat(table_title, "tan series | tan library | ");

    printf("%s \n", table_title);

    /* ==================================================================== */



    /* Creating and printing the line between the title and the table:
    ** ==================================================================== */

    char* second_line = (char*)malloc((strlen(table_title) + 1) * sizeof(char));

    for (int i = 0; i < strlen(table_title) - 1; i++) {
        second_line[i] = '=';
    }

    second_line[strlen(table_title)] = '\0';

    printf("%s \n", second_line);

    /* ==================================================================== */

    free(second_line);

    /* Creating each line of the table and printing it:
    ** ==================================================================== */

    for (int angle = 0; angle < 360; angle += 10) {

        printf("%4i | ", angle);
        printf("%10.5f | %11.5f | ",sin_series(angle), sin(rad(angle)) );
        printf("%10.5f | %11.5f | ",cos_series(angle), cos(rad(angle)) );
        printf("%10.5f | %11.5f | ",tan_series(angle), tan(rad(angle)) );
        printf("\n");
    }

    return 0;   
}

double rad(double x) {
    return ((PI * x) / 180);
}

double next_series_term(double angle, int term_index) {
    double result = 1.0;

    for (int i = 0; i < term_index; i++) {
        result *= angle;
        result /= (i + 1);
    }

    return result;
}

unsigned long long factorial(int x) {

    unsigned long long result = 1;

    for (int i = 0; i < x; i++) {
        result *= (i + 1);
    }

    return result;
}

double sin_series(double x) {

    double result = 0;

    if (x == 0 || x == 180 || x == 360) {
        result = 0;
    }
    else {
        for (int i = 0; i < 100; i++) {

        /* Calculating the next term to add to result to increase precision.*/

        double next_term = next_series_term(rad(x), 2*i + 1);
        next_term *= pow(-1,i);


        result += next_term; 
        }
    }


    return result;  
}

double cos_series(double x) {

    double result = 0;

    if (x == 90 || x == 270) {
        result = 0;
    }
    else {
        for (int i = 0; i < 100; i++) {

        /* Calculating the next term to add to result to increase precision.*/

        double next_term = next_series_term(rad(x), 2*i);
        next_term *= pow(-1,i);     

        result += next_term; 
        }

    }

    return result;  
}

double tan_series(double x) {
        return sin_series(x)/cos_series(x); //non-portable! searching for
                                            // better solution
}   

但是正如我在使用 gdb 后发现的那样,这段代码在返回 0 后会导致段错误,这让我完全困惑不已。作为一个 C 和编程新手,这让我完全困惑。请帮忙。

最佳答案

本声明

char table_title[] = "   n | ";

这将 table_title 声明为一个包含 8 个字符的数组。当您将其他字符串附加到数组末尾时,您将超出范围并有 undefined behavior .

要么指定一个足够大的大小来容纳您需要的所有数据,要么使用完整的字符串正确初始化它。

关于c - 返回零后出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28108526/

相关文章:

无法写入 dsPIC30F OSCCON

c++ - 无法将字符串读入矩阵

c - C 中的二维动态数组分配和按引用传递

c++ - OpenCV 4.2.0 FileStorage段错误

c - C中动态改变结构数组

c - 按位移位 (var Uint8 >> 7) & 0x01u - Misra 兼容

c - 函数指针C语言编程

c - sscanf 匹配 double

c - C中的建筑结构段错误

c - 使用 C (Ubuntu) 的套接字编程中的段错误