所以这里是质量筛子的代码,它提供了正确的输出,正是我想要它做的,除了打印正确结果后的丑陋错误。
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
void prime_gen(int *, int);
void show_primes(unsigned long, unsigned long, int *, int);
int main(void)
{
int no_testcases=0;
int count=0;
unsigned long lower=0,upper=0;
unsigned long size;
size=sqrt(1000000000);
printf("%lu\n", size);
int * primes, *primes_temp;;
primes=(int *)calloc(size+1,sizeof(int));
assert (primes!=NULL);
prime_gen(primes, size+1); /* generates array of 0 and 1's indicating if index is a prime number */
scanf("%d", &no_testcases); /* no of test cases */
while (count<no_testcases)
{
scanf("%lu %lu", &lower, &upper);
show_primes(lower, upper, primes, size+1); /* shows all prime numbers in given range */
count++;
if (count!=no_testcases)
putchar('\n');
}
free(primes);
return 0;
}
输出:
31622
size=31623, set_index=63248, index=31625
1
1
10
2
3
5
7
No of primes = 4
*** glibc detected *** ./a.out: double free or corruption (out): 0x09d49008 ***
a.out: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Przerwane (core dumped)
这是素数生成器和show_primes的代码:
void prime_gen(int * tab, int size) /* 0== is prime, 1== is not prime */
{
tab[0]=1;
tab[1]=1;
unsigned long index=2;
unsigned long set_index;
for(index=2;index<=size;index++)
{
while (index<=size && tab[index]!=0) /* goes to next prime number */
{
index++;
}
for(set_index=index+index;set_index<=size;set_index+=index)
tab[set_index]=1;
}
printf("size=%d, set_index=%lu, index=%lu\n", size, set_index, index);
}
void show_primes(unsigned long l, unsigned long u, int * array, int size)
{
int index = 0;
int is_prime=1;
int primes_count=0;
for(l;l<=u;l++)
{
if (l<=size) /* if l is valid index of array with primes */
{
if(array[l]==0)
{
printf("%lu\n", l);
primes_count++;
}
}
else /* if checked number is bigger than last index of array */
{
is_prime=1;
for (index=2;index<=size;index++)
{
if (array[index]==0)
{
if (l%index==0)
{
is_prime=0;
break;
}
}
}
if (is_prime)
{
printf("%lu\n", l);
primes_count++;
}
}
}
printf("No of primes = %d\n", primes_count);
}
我将它缩小为自由(素数)作为问题,因为当删除错误消失并且程序终止时应该如此。而且我知道数组在这里会更好,但动态数组对我来说是新的,它应该是我对它们的教训。 非常感谢您的回答。
答案 0 :(得分:1)
我的猜测:
void prime_gen(int *, int);
void show_primes(unsigned long, unsigned long, int *, int);
int main(void)
{
int no_testcases=0;
int count=0;
unsigned long lower=0,upper=0;
unsigned long size;
size=sqrt(1000000000);
printf("%lu\n", size);
int * primes, *primes_temp; *primes_head;
primes=(int *)calloc(size+1,sizeof(int));
primes_head = primes;
assert (primes!=NULL);
/* NOTE CHANGE BELOW */
prime_gen(primes, size); /* generates array of 0 and 1's indicating if index is a prime number */
scanf("%d", &no_testcases); /* no of test cases */
while (count<no_testcases)
{
scanf("%lu %lu", &lower, &upper);
/* NOTE CHANGE HERE */
show_primes(lower, upper, primes, size); /* shows all prime numbers in given range */
count++;
if (count!=no_testcases)
putchar('\n');
}
free(primes_head);
return 0;
}
答案 1 :(得分:1)
在prime_gen
函数中,您有:
for(set_index=index+index;set_index<=size;set_index+=index)
tab[set_index]=1;
你的阵列溢出来了。在最后一次迭代中,您写入元素tab[set_index]
,其set_index
值大于size
。
尝试将for
循环停止条件更改为:set_index < size
答案 2 :(得分:1)
您正在使用primes_gen
数组和数组大小调用show_primes
和primes
。在这些函数中,您正在读取并写入超出数组边界的一个函数:
for(set_index=index+index;set_index<=size;set_index+=index)
tab[set_index] = 1;
这会破坏您的阵列,因此您对free()
的呼叫失败。
答案 3 :(得分:0)
尝试将scanf("%lu %lu", &lower, &upper);
移到循环之外