English 中文(简体)
我的法典中的部分编号
原标题:Segmentation Fault in my code

这是在网上法官上提交的一部法典,用于生成主要数字,但Im正在陷入分化错误。 目标是generate prime number between the given range m to n (with n > m)。 采用Etosthenes算法实施。 请告诉我,什么情况会错。 感谢:

#include <stdio.h>
#include <math.h>

int main(){
    long int m,n,c1,c2,c3;
    int t;

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&m,&n);


        //create prime list
        short int *prime;
        prime = (short int*)malloc((n-m)*sizeof(short int));

        //fill list with 0 - prime
        for(c1 = 2; c1 <= n; c1++){
                prime[c1] = 1;
        }

        //set 1 and 0 as not prime
        prime[0]=0;
        prime[1]=0;

        //find primes then eliminate their multiples (0 = prime, 1 = composite)
        for(c2 = 2;c2 <= (int)sqrt(n)+1;c2++){
            if(prime[c2]){
                c1=c2;
                for(c3 = 2*c1;c3 <= n; c3 = c3+c1){
                    prime[c3] = 0;
                }
            }
        }

        //print primes
        for(c1 = m; c1 <=n; c1++){
            if(prime[c1]) printf("%d
",c1);
        }
    }       
    return 0;
}
问题回答

c3 can go up to n in the innermost loop, but you only may allocate less than n slots in your array. In fact, even if you allocated n slots, index n would be one more than the number of slots you allocated. At worst, you d just corrupt some memory past the end of the array and hopefully not trash the stack. At best, I guess you get a segfault. You probably want to change your X <= n to X < n or allocate one more element in your array. In fact, you probably should just allocate (n + 1) * sizeof(short) bytes for your array.

而且,你从未放过头,你从来从未验证用户的投入。 如果是竞争,而竞争会限制投入,则后者可能oka。 另外,你从未放过<代码>prime阵列,因此你有传闻。

Of course it will you are allocating a memory which is (n-m) & you are acessing prime[n] ,

如果只有1个要素的时间长,可以避免:

//set 1 and 0 as not prime
        prime[0]=0;
        prime[1]=0;

You malloc(n-m),但在以下地方,你开始做主[2.n]。 n-m在1E5中,但n本身可以是1E9(相当数量)。 你的简单想法可能无法落实,因为小鼠(1E9)可能失败。 你们需要一种易法算法。





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签