gpt4 book ai didi

c - 段错误(核心转储)-此错误仅有时显示,我似乎无法修复它

转载 作者:行者123 更新时间:2023-11-30 16:25:51 26 4
gpt4 key购买 nike

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

#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()测试时,验证每个部分是否有效,这样我就可以依靠已经编写的代码,专注于手头的部分,以最小的挫败感稳步前进。

<小时/>

最近有很多关于埃拉托色尼筛的问题。我一直发现实现抽象的、动态分配的筛类型以及简单的访问器/修改器函数来更改它很有用。因为程序中应该只有一个筛子,所以可以使用全局变量来描述:

#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() test 函数将 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;
}
}

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

( Verifying 在我的笔记本电脑上,使用单核,一个简单的埃拉托色尼筛找到所有 455,052,511 个小于 10,000,000,000 的素数需要不到 90 秒,并且找到所有 203,280,221 个 32 位素数需要不到 30 秒,以及上面的函数。对于更高的范围,最好使用窗口筛。请注意,素数计数函数不考虑 0 和 1 素数,这与上面的 is_prime() 不同。)

关于c - 段错误(核心转储)-此错误仅有时显示,我似乎无法修复它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53243353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com