c - 内存分配错误 : Thread 1: EXC_BAD_ACCESS (code=2, 地址=0x7fff5f3ffff8)

标签 c arrays pointers malloc

我正在研究一组 40 位数字,并使用这 40 位数字组中的两个作为基本情况来制作斐波那契数列。如果我只使用整数而不使用动态内存的指针,它工作得很好,但是当我实际使用 Int40 结构(它只是一个包含 int*digits 的结构)时,我收到此线程错误。我不知道这意味着什么,我尝试用其他问题来研究它,但没有一个有帮助。谁能帮我?错误发生在第 6 行,使用 ans->digits malloc

感谢您抽出时间!

Int40 *fibKw26(int n, Int40 *first, Int40 *second) {
    int count = 0;
    Int40 *arr = malloc(sizeof(Int40) * 3);
    if (count != n) {
        Int40 *ans = malloc(sizeof(Int40));
        ans->digits = malloc(sizeof(int) * 40);
        arr[0] = *first;
        arr[1] = *second;
        int x[40];
        int y[40];
        int m = 0;
        if (ans == NULL) {
            printf("Malloc error\n");
            exit(-1);
        }
        if (ans->digits == NULL) {
            printf("Malloc error\n");
            exit(-1);
        }
        for (m = 39; m >= 0; m--) {
            x[m] = first->digits[m];
        }
        for (m = 39; m >= 0; m--) {
            y[m] = second->digits[m];
        }
        int temp = 0;
        int carryout = 0;
        int carryin = 0;
        for (m = 39; m >= 0; m--) {
            //printf("%d + %d = ", x[m], y[m]);
            temp = x[m] + y[m];
            if (carryout == 1) {
                carryin = 1;
            }
            if (temp >= 16) {
                //printf(" equal or greater than 16 ");
                temp = temp % 16;
                carryout = 1;
                if (carryin == 1) {
                    //printf(" carried in ");
                    temp++;
                    carryin = 0;
                }
            }
            else {
                carryout = 0;
                if (carryin == 1) {
                    temp++;
                    //printf(" carried in ");
                    carryin = 0;
                    if (temp >= 16) {
                        temp = temp % 16;
                        carryout = 1;
                    }
                }
            }
            ans->digits[m] = temp;
            //printf("%d -- Carryout = %d\n", ans->digits[m], carryout);
        }
        arr[2] = *ans;
        free(ans);
        return fibKw26(n, &arr[1], &arr[2]);
    }
    else {
        return &arr[3];
    }
    return NULL;
}

最佳答案

我假设你的结构 Int40 是这样的:

typedef struct {
    int digits[40];
}Int40;

如果是这种情况,当您这样做时:

Int40 *ans = malloc(sizeof(Int40));

您实际上是在为所有 40 个整数分配内存。所以这样做:

ans->digits = malloc(sizeof(int) * 40);

完全错误且没有必要。还有一件事,假设代码能够以某种方式工作:

    Int40 *ans = malloc(sizeof(Int40));
    ans->digits = malloc(sizeof(int) * 40);
    arr[0] = *first;
    arr[1] = *second;
    int x[40];
    int y[40];
    int m = 0;
    if (ans == NULL) {
        printf("Malloc error\n");
        exit(-1);
    }

您在第 2 行使用了 ans 指针,但您正在代码中测试它是否为 NULL...这是错误的!!!

编辑:我看到的其他东西:

arr[2] = *ans;
free(ans);
return fibKw26(n, &arr[1], &arr[2]);

您正在为 ans 释放内存,但您尝试将指向该内存的指针传递给 fibKw26 函数。你使用arr的方式也是错误的。伙计,你的代码一团糟。如果您不明白指针和内存分配/释放的工作原理,最好使用数组或坐下来学习。

关于c - 内存分配错误 : Thread 1: EXC_BAD_ACCESS (code=2, 地址=0x7fff5f3ffff8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49351422/

相关文章:

android - 在 Android 中以 native 代码创建 .pcap 文件,然后检索它

javascript - NodeJS 对数组中的每个对象进行异步处理

sql - 如何过滤json嵌套键的值

c++ - 双指针的另一种选择

c++ - 使用类的 vector

C++ reinterpret_cast 一个整数

c - 方法中的 "should never get here"行

c - If/else 语句仅适用于整数。

c - 串行通信在 C 中不起作用

java - 将 ArrayList<String> 转换为 String[] 数组