分段错误(核心转储)-部分指针被覆盖,如何解决?
调试的时候发现我需要的指针的一部分被擦除了,导致了这个错误。但我不明白为什么“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已“返回”局部变量的地址
“new_bitset”是一个局部变量,一旦该函数退出,该变量将被释放,保存其值(有效地返回它)是无效的并导致 UB。
要么也分配它,要么在“main”中创建它
You have 'returned' the address of a local variable
'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'