Segmentation fault (core dumped) - 部分指针被覆盖,如何解决?
Segmentation fault (core dumped) - part of the pointer is overwritten, how to solve it?
调试的时候发现我需要的指针被擦掉了一部分,导致了这个错误。但我不明白为什么“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);
}
这是第一次循环前的数据enter image description here
这是错误发生的时间enter image description here
你有'returned'一个局部变量的地址
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.
也可以对其进行 malloc 或在 'main'
中创建它
调试的时候发现我需要的指针被擦掉了一部分,导致了这个错误。但我不明白为什么“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);
}
这是第一次循环前的数据enter image description here
这是错误发生的时间enter image description here
你有'returned'一个局部变量的地址
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.
也可以对其进行 malloc 或在 'main'
中创建它