分段错误(核心转储)-部分指针被覆盖,如何解决?

发布于 2025-01-13 22:10:06 字数 1842 浏览 3 评论 0原文

调试的时候发现我需要的指针的一部分被擦除了,导致了这个错误。但我不明白为什么“i”或“j”声明会这样做...

我的代码

#include <stdio.h>
#include <stdbool.h>
#include <malloc.h>

typedef unsigned long bitset_index_t;

typedef struct
{
    bitset_index_t index;
    unsigned short *vyraz;

} bitset_t;

void bitset_alloc(bitset_t **pole, unsigned int velikost)
{
    if (velikost < 1)
    {
        // todo error
    }
    bitset_t new_bitset = {.index = 0, .vyraz = NULL};
    new_bitset.vyraz = malloc(sizeof(unsigned short) * velikost);
    if (new_bitset.vyraz == NULL)
    {
        // todo error
    }
    memset(new_bitset.vyraz, 0, sizeof(unsigned short) * (velikost));

    for (unsigned int i = 0; i < velikost; ++i)
    {

        new_bitset.vyraz[i] = 0;
        new_bitset.index++;
    }
    *pole = &new_bitset;
}

void Eratosthenes(bitset_t *pole)
{
    pole->vyraz[0] = 1;
    pole->vyraz[0] = 1;

    unsigned long velikost = pole->index;
    for (unsigned long i = 2; i < velikost; i++)
    {
        for (unsigned long j = i; j * i < velikost + 1; j++)
        {
            if (pole->vyraz[i])
                break;
            pole->vyraz[i * j] = 1;
        }
    }
    unsigned short count_prvocisel = 0;
    for (unsigned long i = pole->index; i > 0; i--)
    {
        if (count_prvocisel < 10)
        {
            if (pole->vyraz[i])
                continue;
            printf("%lu ", i);
            count_prvocisel++;
        }
        else
            break;
    }
}

int main()
{
    bitset_t *test = NULL;

    bitset_alloc(&test, 10);

    Eratosthenes(test);
}

这是第一个周期之前的数据 在此处输入图像描述

这是发生错误时的输入图片说明在这里

When debugging, I found out that part of the pointer I need is erased and this error is caused. But I don't understand why the "i" or "j" declaration does this...

My code

#include <stdio.h>
#include <stdbool.h>
#include <malloc.h>

typedef unsigned long bitset_index_t;

typedef struct
{
    bitset_index_t index;
    unsigned short *vyraz;

} bitset_t;

void bitset_alloc(bitset_t **pole, unsigned int velikost)
{
    if (velikost < 1)
    {
        // todo error
    }
    bitset_t new_bitset = {.index = 0, .vyraz = NULL};
    new_bitset.vyraz = malloc(sizeof(unsigned short) * velikost);
    if (new_bitset.vyraz == NULL)
    {
        // todo error
    }
    memset(new_bitset.vyraz, 0, sizeof(unsigned short) * (velikost));

    for (unsigned int i = 0; i < velikost; ++i)
    {

        new_bitset.vyraz[i] = 0;
        new_bitset.index++;
    }
    *pole = &new_bitset;
}

void Eratosthenes(bitset_t *pole)
{
    pole->vyraz[0] = 1;
    pole->vyraz[0] = 1;

    unsigned long velikost = pole->index;
    for (unsigned long i = 2; i < velikost; i++)
    {
        for (unsigned long j = i; j * i < velikost + 1; j++)
        {
            if (pole->vyraz[i])
                break;
            pole->vyraz[i * j] = 1;
        }
    }
    unsigned short count_prvocisel = 0;
    for (unsigned long i = pole->index; i > 0; i--)
    {
        if (count_prvocisel < 10)
        {
            if (pole->vyraz[i])
                continue;
            printf("%lu ", i);
            count_prvocisel++;
        }
        else
            break;
    }
}

int main()
{
    bitset_t *test = NULL;

    bitset_alloc(&test, 10);

    Eratosthenes(test);
}

this is the data before the first cycle enter image description here

this is at the time of the error enter image description here

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

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

发布评论

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

评论(1

橘味果▽酱 2025-01-20 22:10:06

您已“返回”局部变量的地址

void bitset_alloc(bitset_t** pole, unsigned int velikost)
{
    if (velikost < 1)
    {
        // todo error
    }
    bitset_t new_bitset = { .index = 0, .vyraz = NULL }; <<<<========
    new_bitset.vyraz = malloc(sizeof(unsigned short) * velikost);
    if (new_bitset.vyraz == NULL)
    {
        // todo error
    }
    memset(new_bitset.vyraz, 0, sizeof(unsigned short) * (velikost));

    for (unsigned int i = 0; i < velikost; ++i)
    {

        new_bitset.vyraz[i] = 0;
        new_bitset.index++;
    }
    *pole = &new_bitset; <<<<<=========

}

“new_bitset”是一个局部变量,一旦该函数退出,该变量将被释放,保存其值(有效地返回它)是无效的并导致 UB。

要么也分配它,要么在“main”中创建它

You have 'returned' the address of a local variable

void bitset_alloc(bitset_t** pole, unsigned int velikost)
{
    if (velikost < 1)
    {
        // todo error
    }
    bitset_t new_bitset = { .index = 0, .vyraz = NULL }; <<<<========
    new_bitset.vyraz = malloc(sizeof(unsigned short) * velikost);
    if (new_bitset.vyraz == NULL)
    {
        // todo error
    }
    memset(new_bitset.vyraz, 0, sizeof(unsigned short) * (velikost));

    for (unsigned int i = 0; i < velikost; ++i)
    {

        new_bitset.vyraz[i] = 0;
        new_bitset.index++;
    }
    *pole = &new_bitset; <<<<<=========

}

'new_bitset' is a local variable that will be released once this function exits, saving its value - effectively returning it - is invalid and results in UB.

Either malloc it too or create it in 'main'

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