0

error message:
Program stopped at 0x77dc2f6a
It stopped with signal SIGSEGV, Segmentation fault.
(gdb) Using the running image of child thread 2120.0x20d8. Program stopped at 0x77dc2f6a
It stopped with signal SIGSEGV, Segmentation fault.

complier: C-Free

here is my codes

#include<stdio.h>

using namespace std;

/*
128m
n: [6,40] k: [1,6]
n*k: 0.9k
*/

struct BN
{
    char num[160];
    int len;
};

int len, num_k;
BN number;
BN* map;
bool* isC;

void BN_get(BN* bn, int len);
void BN_show(BN* bn);
void BN_clear(BN* bn);

// compare
bool BN_gt(BN* a, BN* b);

// count
void BN_mul(BN* a, BN* b, BN* c, int aS, int aE, int bS, int bE);//c = a+b

void f(int n, int k);

int main()
{
    int i, j;
    char c;

    scanf("%d  %d", &len, &num_k);
    BN_get(&number, len);

    map = new BN[len*num_k];
    isC = new bool[len*num_k];

    for(i=0;i<len*num_k;++i)
        isC[i]=false;

    // init
    for(i=0; i<len; ++i){
        //map[i,0]
        for(j=0;j<=i;++j){
            map[i*num_k].num[j] = number.num[j];
        }
        map[i*num_k].len = i+1;
        isC[i*num_k] = true;
    }

    //printf("");//case1
    f(len-1, num_k);

    BN_show(&map[len*num_k]);

    return 0; // line 63
}

void BN_get(BN* bn, int len)
{
    int i;
    char c;
    c = getchar();
    for(i=len-1;i>=0;--i){
        scanf("%c", &c);
        if(i>159 || i<0)printf("OUT\n");//debug
        bn->num[i] = c-'0';
    }// line 85
    bn->len = len;
}

void BN_show(BN* bn)
{
    int i, len=bn->len;
    for(i=len-1;i>=0;--i){
        if(i>159 || i<0)printf("OUT\n");//debug
        printf("%d", bn->num[i]);
    }
}

void BN_clear(BN* bn)
{
    int i;
    for(i=0;i<160;++i){
        if(i>159 || i<0)printf("OUT\n");//debug
        bn->num[i]=0;
    }

    bn->len = 0;
}

bool BN_gt(BN* a, BN* b)
{
    int i, an, bn;
    int alen = a->len, blen=b->len;
    // if a>b
    if(blen>alen){
        return false;
    }else if(alen==blen){
        for(i=alen-1; i>=0; --i){
            if(i>159 || i<0)printf("OUT\n");//debug
            an = a->num[i];
            bn = b->num[i];
            if(an>bn)
                return true;
            if(an<bn)
                return false;
        }
        return false;
    }
    return true;
}

void BN_mul(BN* a, BN* b, BN* c, int aS, int aE, int bS, int bE)
{
    int i, j, k, l;
    char *an=a->num, *bn=b->num, *cn=c->num;

    BN_clear(c);

    for(i=bS, k=0; i<=bE; ++i, ++k){
        for(j=aS,l=k; j<=aE; ++j,++l){
            if(i>159 || i<0)printf("OUT\n");//debug
            if(l>159 || l<0)printf("OUT\n");//debug
            if(j>159 || j<0)printf("OUT\n");//debug
            if(l+1>159 || l+1<0)printf("OUT\n");//debug
            cn[l] += bn[i]*an[j];
            cn[l+1] += cn[l]/10;
            cn[l] %= 10;
        }
    }
    if(l>159 || l<0)printf("OUT\n");//debug
    if(cn[l]==0)
        c->len = l;
    else
        c->len = l+1;
}

void f(int n, int k)
{
    int i, up=n-1-(k-2);
    BN max, a, b;

    BN_clear(&max);

    for(i=0;i<up;++i){
        // b = f(n-1-i, k-1)
        if(!isC[(n-1-i)*num_k+k-1])
            f(n-1-i, k-1);
        b = map[(n-1-i)*num_k+k-1];

        // a = f(n-1-i, k-1) x num(n-i, n);
        BN_mul(&b, &number, &a, 0, b.len-1, n-i, n);

        // max
        if(BN_gt(&a, &max))
            max=a;
    }

    isC[n*num_k+k] = true;
    map[n*num_k+k] = max;
}

BN_xxx() is some op of big number
In the end of main, here is line of code

"//printf("");//case1"

If i don't printf(), i will received the wrong message.
If i printf(), even print empty, it will run without wrong.

I set lots of check, and it seens that never out of range.
I guess the problom is about IO stream, but i don't know what the happen.

I run in msvs again. Here is the stack

name                            language
ntdll.dll!00007ffbc22d4cfa()    unknown
ntdll.dll!00007ffbc22dc806()    unknown
ntdll.dll!00007ffbc22dcad1()    unknown
ntdll.dll!00007ffbc2279a55()    unknown
ntdll.dll!00007ffbc21f6db5()    unknown
ntdll.dll!00007ffbc21f2b55()    unknown
ucrtbased.dll!00007ffb6a341716()    unknown
ucrtbased.dll!00007ffb6a3414cd()    unknown
ucrtbased.dll!00007ffb6a34455f()    unknown
ucrtbased.dll!00007ffb6a3a622b()    unknown
ucrtbased.dll!00007ffb6a368e15()    unknown
ucrtbased.dll!00007ffb6a3857c6()    unknown
ucrtbased.dll!00007ffb6a37c085()    unknown
ucrtbased.dll!00007ffb6a37c27b()    unknown
ucrtbased.dll!00007ffb6a37cbd7()    unknown
ucrtbased.dll!00007ffb6a39ea3b()    unknown
ConsoleApplication1.exe!_vfprintf_l(_iobuf * const _Stream, const char * const _Format, __crt_locale_pointers * const _Locale, char * _ArgList) line 644    C++
ConsoleApplication1.exe!printf(const char * const _Format, ...) line 958    C++
ConsoleApplication1.exe!BN_show(BN * bn) line 85    C++
ConsoleApplication1.exe!main() line 63  C++
windgodm
  • 1
  • 2
  • 1
    You ran in a debugger, and that's good, but *where* did the crash happens? If you list your function call stack, where in your code is the crash? Please tell us and also please edit the code to include a comment on that line. – Some programmer dude Oct 05 '18 at 11:48
  • I run in msvs again, and it look's ............. – windgodm Oct 05 '18 at 12:08

1 Answers1

0

You allocate memory:

map = new BN[len*num_k];

That effectively creates an array with indexes 0 to len*num_k - 1 (inclusive), and make map point to the first element of that array.

Then you call BN_show:

BN_show(&map[len*num_k]);

To the function you pass a pointer to the structure at index len*num_k which is one beyond the end of your allocated array! Going out of bounds leads to undefined behavior.

The pointer you pass to the BN_show function is simply not valid, and you need to modify your code to pass a pointer to a valid structure.

If you want to print all elements of the map array, then use a loop over it, and call BN_show for each element separately.


On a related side-note: Don't do manual allocations of arrays, use std::vector instead.

I also recommend you stop using pointers, and instead use either object ("values") directly, or references.

Lastly, please get a few good books on C++ and learn it properly. Right now it seems you're using it more as an enhanced C compiler.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • oh, thanks. The range of map should be len*(num_k+1). This code is to solve an dp problom. Now the problom has been solved. Thanks very muck. You are so careful. Thanks so much!! – windgodm Oct 05 '18 at 13:00
  • 1
    Thanks for your advice, it seems that the road ahead will be long, i'll keep working hard. – windgodm Oct 05 '18 at 13:07