D SEGFAULT(核心转换)

时间:2013-12-21 22:59:52

标签: segmentation-fault d

我在D中制作了一个程序来生成(n + 1)个第一个Stern-Brocot序列。设SB(n)为(n + 1)Stern-Brocot序列。我将SB(n)存储到名为“n.txt”的文件中。 SB(n)包含(2 ^ n)+ 1个元素。

程序很简单,我们要求整数n,我们生成SB(0),SB(1)...... SB(n)。 问题是,当n> = 15时,我得到:“Segfault(core dumped)”。

代码是:

import std.stdio;
import std.math;
import std.string;
import std.file;
import std.regex;

struct rational{
    int num;
    int den;
};
alias rational rat;

int main(){
    int n;
    write("n : ");
    readf(" %s", &n);
    SB(n);
    return 0;
}

void SB(int n){
    int cn = 0;
    File file = File("0.txt", "w");
    file.write("0/1\n1/0");
    file.close;
    while(cn < n)
    {
        writeln("Generating SB(",cn+1,").");
        File from = File(_cast(cn)~".txt", "r");
        File to = File(_cast( cn+1)~".txt", "w");
        string A;
        string B;
        A = chomp(from.readln());
        rat a = extract(A);
        rat b;
        to.write(A);
        while(!from.eof())
        {
            B = chomp(from.readln());
            b = extract(B);
            rat q = oplus(a,b);
            to.write("\n", q.num, "/", q.den, "\n");
            to.write(B);
            a = b;
        }
        from.close;
        to.close;
        cn++;
    }
}

string _cast(int n){
    string s;
    if (n == 0) return "0";
    int nb = cast (int) trunc(log(cast (real) n) / log(10));
    int [] num;
    while(nb >= 0)
    {
        num ~= n / 10^^(nb);
        n -= num[$-1] * 10^^(nb);
        nb--;
    }
    char [] c;
    foreach(i; num)
    {
        switch (i)
        {
            case 0 : c ~= '0';
            break;
            case 1 : c ~= '1';
            break;
            case 2 : c ~= '2';
            break;
            case 3 : c ~= '3';
            break;
            case 4 : c ~= '4';
            break;
            case 5 : c ~= '5';
            break;
            case 6 : c ~= '6';
            break;
            case 7 : c ~= '7';
            break;
            case 8 : c ~= '8';
            break;
            case 9 : c ~= '9';
            break;
            default : writeln("Bad symbol encountered : ", i,".");
            break;
        }
    }
    return cast (string) c;
}

int __cast(string s){
    char [] c = cast (char[]) s;
    int [] num;
    foreach(i; c)
    {
        switch (i)
        {
            case '0' : num ~= 0;
            break;
            case '1' : num ~= 1;
            break;
            case '2' : num ~= 2;
            break;
            case '3' : num ~= 3;
            break;
            case '4' : num ~= 4;
            break;
            case '5' : num ~= 5;
            break;
            case '6' : num ~= 6;
            break;
            case '7' : num ~= 7;
            break;
            case '8' : num ~= 8;
            break;
            case '9' : num ~= 9;
            break;
            default : writeln("Bad symbol encountered : ", i,".");
            break;
        }
    }
    num = num.reverse;
    int a = 0;
    for (int i = 0; i < num.length; i++)
    {
        a += num[i] * 10^^i;
    }
    return a;
}

rat extract(string s){
    s = chomp(s);
    auto r = regex("([0-9]+)/([0-9]+)$");
    auto m = match(s, r);
    return simple(rat(__cast(m.captures[1]), __cast(m.captures[2])));
}

rat oplus(rat q1, rat q2){
    q1 = simple(q1);
    q2 = simple(q2);
    return simple(rat(q1.num + q2.num, q1.den + q2.den));
}

int gcd(int a, int b){
    int r = 1;
    if (a == 0 && b == 0) return 1;
    if (a == 0) return b;
    if (b == 0) return a; 
    while(r != 0)
    {
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

rat simple(rat q){
    int g = gcd(q.num, q.den);
    return rat(q.num / g , q.den / g);
}

我不知道为什么我会得到这个Segfault。你知道为什么吗?

我的D编译器是LDC。

核心档案:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a89742 in _aaInX (aa=<incomplete type>, 
    keyti=0x482e80 <TypeInfo_S3std8typecons18__T5TupleTAyaTAxaZ5Tuple.__init()>, pkey=0x7fffffffd610) at aaA.d:371
371             auto e = aa.a.b[i];
(gdb) bt
#0  0x00007ffff7a89742 in _aaInX (aa=<incomplete type>, 
    keyti=0x482e80 <TypeInfo_S3std8typecons18__T5TupleTAyaTAxaZ5Tuple.__init()>,    pkey=0x7fffffffd610) at aaA.d:371
#1  0x000000000044e44a in    std.functional.__T7memoizeS83_D3std5regex18__T9regexImplTAyaZ9regexImplFNfAyaAxaZS3std5regex 12__T5RegexTaZ5RegexVi8Z.memoize() (
    _param_0=..., _param_1=...) at /usr/include/d/std/functional.d:602
#2  0x000000000042f5aa in std.regex.__T5regexTAyaZ.regex() (pattern=...,flags=...) at /usr/include/d/std/regex.d:6461
#3  0x000000000042451e in dev.extract() (s=...)
    at /home/jonathan/Documents/Recherche/Fermat/SBs/D/Dev/dev.d:134
#4  0x00000000004238cb in dev.SB() (n=15)
    at /home/jonathan/Documents/Recherche/Fermat/SBs/D/Dev/dev.d:39
#5  0x0000000000423547 in D main ()
    at /home/jonathan/Documents/Recherche/Fermat/SBs/D/Dev/dev.d:17
#6  0x00007ffff7a82ab1 in rt.dmain2._d_run_main() (mainFunc=0x423500 <D main>)
    at dmain2.d:607
#7  0x00007ffff7a82a3a in rt.dmain2._d_run_main() () at dmain2.d:582
#8  0x00007ffff7a82b09 in rt.dmain2._d_run_main() () at dmain2.d:618
#9  0x00007ffff7a82a3a in rt.dmain2._d_run_main() () at dmain2.d:582
#10 0x00007ffff7a829ed in _d_run_main (argc=1, argv=0x7fffffffe148, 
    mainFunc=0x423500 <D main>) at dmain2.d:628
#11 0x00007ffff7a825a2 in main (argc=1, argv=0x7fffffffe148) at dmain2.d:383
#12 0x00007ffff6a64b45 in __libc_start_main (main=0x422cc0 <main@plt>, argc=1,  ubp_av=0x7fffffffe148, init=<optimized out>, fini=<optimized out>,rtld_fini=<optimized out>, stack_end=0x7fffffffe138) at libc-start.c:274
#13 0x0000000000423439 in _start ()

1 个答案:

答案 0 :(得分:0)

您使用的是Fedora存储库LDC吗?

如果是这样,请尝试使用其他D编译器,或尝试使用不同的LDC版本。 Fedora的LDC使用共享库,Phobos不支持这些库并导致段错误。