c-分段错误(核心转储)-这个错误只是偶尔出现,我似乎无法修复它



我在这里和其他地方看到了许多不同的论坛,我似乎找不到我的代码有什么问题。我没有指针,所以它只能是我使用的数组中的一些东西——我试图更改它们,看看发生了什么,试图写下发生了什么以及何时发生,但有时仍然会显示错误。我只知道这与记忆和访问一些我不应该访问的东西有关,但无法识别问题——可能更多。

#include <stdio.h>
#include <math.h>
typedef int bool;
#define true 1
#define false 0
int main(int argc, char *argv[]){
int number;
int test;
while ((test = scanf("%d", &number)) == 1){
if (number == 0){
return 0;
}
if (number < 0){
fprintf(stderr, "Error: Chybny vstup!n");
return 100;
}
printf("Prvociselny rozklad cisla %d je:n", number);
if (number == 1){
printf("%d", number);
}
else{
bool prime[number+1];
for(int i = 0; i <= number; i++){
prime[i] = true;
}
for(int j = 2; j * j <= number; j++){
if (prime[j] == true){
for (int multiples = 2; prime[j * multiples] <= number; multiples++){
prime[j * multiples] = false;
}
}
}
int result[50];
int multipliers[50];
for(int i = 0; i <= 50; i++){
result[i] = 0;
}
for(int i = 0; i <= 50; i++){
multipliers[i] = 1;
}
int counter = 0;
for (int test=2; test <= number; test++){
if (prime[test]){
if (number % test == 0){
number /= test;
result[counter] = test;
counter++;
while(number % test == 0){
multipliers[counter-1]++;
number /= test;
}
}
}
}
for (int c = 0; c < counter; c++){
if (multipliers[c] > 1){
printf("%d^%d", result[c], multipliers[c]);
}
if (multipliers[c] == 1){
printf("%d", result[c]);
}
if (result[c+1] > 1){
printf(" x ");
}
}
}
printf("n");
}
if (test == 0){
fprintf(stderr, "Error: Chybny vstup!n");
return 100;
}
}

您应该始终分段编写程序,并在完成每个功能后验证它是否工作,然后再转到新功能。由于您将分别测试正在实现的每个功能,因此您知道任何错误都很可能出现在上次实现的功能中。

在你的情况下,这个问题立即引起了我的注意:

for (int multiples = 2; prime[j * multiples] <= number; multiples++){
prime[j * multiples] = false;
}

在这里,您测试prime[j * multiples] <= number,即将标志与一个数字进行比较。由于延迟标志在数组结束前一直为零,因此j*multiples将超出prime[]数组的范围,并最终导致程序崩溃。

当然,测试应该是j * multiples <= number

因为我一次处理一个功能和一个问题,我不知道这是否是你的代码唯一的问题。找出答案是你的任务。如果我是你,我会分段编写代码,也许在测试时会喷洒printf()s,以验证每个部分的工作情况,这样我就可以依赖我已经编写的代码,专注于手头的部分,并以最小的挫折感稳步前进。


最近有不少关于Eratosthenes筛的问题。我一直觉得实现一个抽象的、动态分配的筛选类型和简单的访问器/修饰符函数来更改它很有用。因为一个程序中应该只有一个筛选,所以可以使用全局变量来描述它:

#include <stdlib.h>
#include <inttypes.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
/* This is the number of bits in an unsigned long.
The exact value is floor(log(ULONG_MAX + 1)/log(2)),
but the following definition will be correct on all
modern architectures: */
#define  ULONG_BITS  (sizeof (unsigned long) * CHAR_BIT)
/* Prime sieve, with a flag for zero and each odd integer,
one bit per flag. */
static unsigned long  *sieve = NULL;
/* Largest positive integer within the sieve. */
static uint64_t        sieve_max = 4;
/* Number of unsigned longs allocated and initialized in the sieve. */
static size_t          sieve_words = 0;
/* Function to discard the sieve, if no longer needed. */
static inline void  sieve_free(void)
{
free(sieve);        /* free(NULL); is safe, and does nothing. */
sieve       = NULL;
sieve_max   = 4;    /* Since 0, 1, 2, 3, 4 give built-in answers */
sieve_words = 0;
}

初始sieve_max是4,因为我们的is_prime()测试函数处理0、1、2和3素数,以及所有更大的偶数整数都不是素数;这意味着我们的is_prime():总是知道0到4的整数

/* Return 1 if number is prime according to sieve,
0 if known composite/not prime. */
static inline  int is_prime(const uint64_t  number)
{
/* 0, 1, 2, and 3 are considered prime. */
if (number <= 3)
return 1; /* Prime */
/* All larger even integers are not prime. */
if (!(number & 1))
return 0; /* Not prime */
/* Outside the sieve? */
if (number > sieve_max) {
fprintf(stderr, "is_prime(): %" PRIu64 " is outside the sieve!n", number);
exit(EXIT_FAILURE);
}
{
const uint64_t       half = number / 2;
const size_t         w = half / ULONG_BITS;
const unsigned long  m = 1uL << (half % ULONG_BITS);
/* The flag for odd number is (number/2)th.
half / ULONG_BIT  is the word where bit
half % ULONG_BIT  is in.
Return 0 if the bit is set, 1 if clear. */
return !(sieve[w] & m);
}
}

我们可以很容易地扩大筛子,将所有新标志默认为"非素数":

static void sieve_upto(const uint64_t  max)
{
/* Number of words needed for (max+1)/2 bits. */
const uint64_t nwords = 1 + max / (2 * ULONG_BITS);
const uint64_t nbytes = nwords * (uint64_t)sizeof (unsigned long);
const size_t   words = (size_t)nwords;
const size_t   bytes = words * sizeof (unsigned long);
unsigned long *temp;
/* Already covered by the current sieve? */
if (sieve_max > max)
return;
/* We need to be able to handle max+1,
and neither bytes or words should overflow. */
if (max >= UINT64_MAX ||
(uint64_t)words != nwords || (uint64_t)bytes != nbytes) {
fprintf(stderr, "sieve_upto(): %" PRIu64 " is too high a maximum.n", max);
exit(EXIT_FAILURE);
}
/* Do we already have all the bytes we need? */
if (words == sieve_words) {
sieve_max = max;
return;
}
/* Allocate/reallocate. Note that realloc(NULL, size)
is equivalent to malloc(size), so this is safe
even if 'sieve' is NULL. */
temp = realloc(sieve, bytes);
if (!temp) {
fprintf(stderr, "Not enough memory for a sieve up to %" PRIu64 ".n", max);
exit(EXIT_FAILURE);
}
sieve = temp;
/* Clear the new words to all zeros. */
memset(sieve + sieve_words, 0, (words - sieve_words) * sizeof (unsigned long));
sieve_max   = max;
sieve_words = words;
}

最后,我们需要一个函数来标记一个数字复合(而不是素数(:

static inline void not_prime(const uint64_t  number)
{
/* Primality is internally handled for 0, 1, 2, 3, and 4. */
if (number <= 4)
return;
/* All larger even numbers are internally handled. */
if (!(number & 1))
return;
/* Outside the sieve? */
if (number > sieve_max) {
fprintf(stderr, "not_prime(): %" PRIu64 " is outside the sieve!n", number);
exit(EXIT_FAILURE);
}
{
const uint64_t       half = number / 2;
const size_t         w = half / ULONG_BITS;
const unsigned long  m = 1uL << (half % ULONG_BITS);
/* Half is the bit index in sieve[].
half / ULONG_BITS  is the word containing the bit
half % ULONG_BITS  that needs to be set. */
sieve[w] |= m;
}
}

埃拉托斯梯尼结构的筛子我将留给你;我的目的只是展示如何使用动态内存管理来创建和管理一个相对高效的(即平衡内存使用和运行时(奇数正整数筛选。

(在我的笔记本电脑上,使用单核和上述函数,验证一个简单的Eratosthenes筛子是否能找到所有小于10000000000的455052511素数需要不到90秒的时间,以及它是否能找到全部203280221 32位素数需要不超过30秒的时间。对于更高的范围,最好使用带窗口的筛子。注意,素数计数函数不考虑0和1素数,与abo不同veis_prime().(

最新更新