【问题标题】:Error with Memory Allocation: Thread 1: EXC_BAD_ACCESS (code=2, address=0x7fff5f3ffff8)内存分配错误:线程 1:EXC_BAD_ACCESS(代码=2,地址=0x7fff5f3ffff8)
【发布时间】:2018-08-27 07:57:40
【问题描述】:

我正在研究一组 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;
}

【问题讨论】:

标签: c arrays pointers malloc


【解决方案1】:

我假设你的结构 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 的方式也是错误的。伙计,你的代码一团糟。如果您不了解指针和内存分配/释放的工作原理,最好使用数组或坐下来学习。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    相关资源
    最近更新 更多