我必须询问我从这个代码中获得的中止(核心倾倒)错误

发布于 2025-01-30 07:15:23 字数 672 浏览 2 评论 0原文

执行此代码时,我遇到了一个segfault错误。它打印了5个数字中最大的数字,并且这些数字存储在堆中。

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

int main() {
    int *ptr = (int *)malloc(5 * sizeof(int));
    *ptr = 5;
    *(ptr + 1) = 7;
    *(ptr + 2) = 2;
    *(ptr + 3) = 9;
    *(ptr + 4) = 8;

    int *ptr_max = (int *)malloc(sizeof(int));
    *ptr_max = 0;

    for (int i = 0; i < 5; i++) {
        if (*ptr_max < *ptr) {
            *ptr_max = *ptr;
            ptr++;
        } else
            ptr++;
    }
    printf("%d\n", *ptr_max);
    free(ptr);
    free(ptr_max);
    return 0;
}

我想知道为什么我从上面的代码中遇到了这个错误。请任何人向我解释吗?

I got a segfault error while executing this code. It prints the largest of 5 numbers, and those numbers are stored in heap-memory.

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

int main() {
    int *ptr = (int *)malloc(5 * sizeof(int));
    *ptr = 5;
    *(ptr + 1) = 7;
    *(ptr + 2) = 2;
    *(ptr + 3) = 9;
    *(ptr + 4) = 8;

    int *ptr_max = (int *)malloc(sizeof(int));
    *ptr_max = 0;

    for (int i = 0; i < 5; i++) {
        if (*ptr_max < *ptr) {
            *ptr_max = *ptr;
            ptr++;
        } else
            ptr++;
    }
    printf("%d\n", *ptr_max);
    free(ptr);
    free(ptr_max);
    return 0;
}

I want to know why exactly I got this error from the above code. Please can anyone explain it to me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

又怨 2025-02-06 07:15:23

当您是free() -ing ptr时,真正的问题就在于。一旦,您将其地址增加到指针跳到由malloc()分配的下一个地址。始终确保*ptrptr [0]相同。因此,要解决此问题,您可以将ptr降低5,或创建复制的指针。

给出free()的地址的示例,它们没有指向相同的内存块:

Before decrementing 0x213f2b4
After decrementing 0x213f2a0

通过5降低的原因是这两个十六进制20的值,与sizeof(int) * 5相同

ptr -= 5;

,或者

您可以创建指针的副本,然后对复制的操作执行操作:

int *my_copied_ptr = ptr; // you don't need to free this pointer

然后,free()他们:

free(ptr);
free(ptr_max);

现在,为了在大型代码库中进一步避免这些错误,请尝试使用[]运算符,例如:

ptr[0] = 5;
ptr[1] = 7;
ptr[2] = 2;
ptr[3] = 9;
ptr[4] = 8;

The real problem lies when you are free()-ing the ptr. Once, you increment a pointer its address jumps to next address allocated by malloc(). Always make sure that *ptr is same as ptr[0]. So, to fix this issue, you can decrement the ptr by 5, or create a copied pointer.

Example of address given to free(), they are not pointing to the same memory block:

Before decrementing 0x213f2b4
After decrementing 0x213f2a0

The reason for decrementing by 5, is the difference between these two hexadecimal values which is 20, same as sizeof(int) * 5.

ptr -= 5;

OR

You can create a copy of your pointer and then perform operations on that copied one:

int *my_copied_ptr = ptr; // you don't need to free this pointer

Then, free() them:

free(ptr);
free(ptr_max);

Now, to avoid these mistakes further in a large code bases, try using [] operator like this:

ptr[0] = 5;
ptr[1] = 7;
ptr[2] = 2;
ptr[3] = 9;
ptr[4] = 8;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文