首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >得到一个"EXC_BAD_ACCESS“错误,我不知道为什么

得到一个"EXC_BAD_ACCESS“错误,我不知道为什么
EN

Stack Overflow用户
提问于 2013-11-13 16:15:41
回答 1查看 39关注 0票数 0

这是我的密码。它编译得很好,我根本看不出可能出了什么问题:

代码语言:javascript
复制
    // This program takes a quadratic from the user, and prints the solution(s) if they exist. 

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


//Make sign function for switch test - because in the switch expression itself, C refused to believe x/|x| is an integer...
int sign(float f){
    printf("\n\nSIGN CALL OK\n\n");
    int sign=f/cabs(f);
    return sign;
}

// Define quadratic structure 
typedef struct quadratic{
    float a, b, c;
    float discriminant;
    float real_root_1;
    float real_root_2;
    float complex_root;
} Quadratic;

// 'Redefine' malloc to also check allocation of memory when called.

Quadratic* xmalloc(size_t n){
    printf("\n\nXMALLOC CALL OK\n\n");
    Quadratic* p = malloc(n);
    if (p == NULL) {
        printf("\n ERROR: Unable to allocate memory! \n");
        exit(1);
    }
    return p;
}

// newquadratic lets the user define the quadratic to be solved, and returns the pointer to its structure. 

Quadratic* newquadratic() {
    Quadratic* q = xmalloc(sizeof *q);
    printf("\n Please enter the coefficients of the quadratic separated by spaces: ");
    scanf("%g, %g, %g", q->a, q->b, q->c);
    printf("\n\nDATA ACCEPTED\n\n");
    return q;
}

// solve takes the existing data from the quadratics structure and defines the remaining quantities, depending on which
// case we get for the 'sign of the discriminant'.

int solve(Quadratic eqn){
    printf("\n\nSOLVE CALL OK\n\n");
    eqn.discriminant = (eqn.b*eqn.b - 4*eqn.a*eqn.c);

    switch (sign(eqn.discriminant)) {
        case -1:
            eqn.real_root_1 = (-eqn.b+sqrt(eqn.discriminant))/(2*eqn.a);
            eqn.real_root_2 = (-eqn.b-sqrt(eqn.discriminant))/(2*eqn.a);
            break;
        case 0:
            eqn.real_root_1 = (-eqn.b+sqrt(eqn.discriminant))/(2*eqn.a);
            break;
        case 1:
            eqn.complex_root = (-eqn.b+sqrt(eqn.discriminant))/(2*eqn.a);
            break;
    }
    return sign(eqn.discriminant);
}

//main also uses sign of the discriminant (as returned by solve) to decide how to behave appropriately for the case it receives.

int main () {

    Quadratic* eqn=newquadratic();

    printf("\n\n GET QUADRATIC OK\n\n");

    switch (solve(*eqn)) {
        case -1:
            printf("\n\n We have two real solutions, %g and %g", eqn->real_root_1, eqn->real_root_2);
            break;
        case 0:
            printf("\n\n We have one repeated real solution, %g", eqn->real_root_1);
            break;
        case 1:
            printf("\n\n We have two complex solutions, %g%+gi and %g%-gi", creal(eqn->complex_root),cimag(eqn->complex_root),creal(eqn->complex_root),(-1)*cimag(eqn->complex_root));
            break;
    }

        return 0;
}

xmalloc调用正确,我们就进入了用户输入3个系数的阶段。一旦返回被推送,我们就会得到错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-13 16:54:48

问题在于你的函数是新二次的。

当您实际传递变量所保存的值时,scanf函数希望得到它应该将结果放入的变量的地址。

要修复,请传递a、b和c的地址,如下所示:(添加&字符)

代码语言:javascript
复制
Quadratic* newquadratic() 
{
    Quadratic* q = xmalloc(sizeof *q);
    printf("\n Please enter the coefficients of the quadratic separated by spaces: ");
    scanf("%g, %g, %g", &q->a, &q->b, &q->c);
    printf("\n\nDATA ACCEPTED\n\n");
    return q;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19958850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档