为什么这个功能会一直崩溃?

时间:2015-11-28 05:40:05

标签: c++ hash compression

ForeTHought:如果需要,那么我可以添加类定义。 问题:每当我尝试在程序中运行此函数时,我都会收到STATUS_ACCESS_VIOLATION。我想知道发生了什么事。我在某个地方出界吗?如果我能自己解释一下,我愿意。但我无法独自解决这个问题。我非常接近雇用某人为我做调试。值得我的诡计。所以无论如何,这需要被重新审视并给出一点TLC。提前谢谢!

int S_Rend::count(bitset<8> alpha, bitset<8> spec) {

    int bn;
    vector< bitset<8> > cnt;
    bitset<8> curr;
    int chmp;

    eta = (alpha & spec);
    theta = (alpha | spec);

    cnt[0] = eta & alpha;
    cnt[1] = eta | alpha;
    cnt[2] = eta & spec;
    cnt[3] = eta | spec;
    cnt[4] = theta & alpha;
    cnt[5] = theta | alpha;
    cnt[6] = theta & spec;
    cnt[7] = theta | spec;
    cnt[8] = cnt[0] & cnt[5];
    cnt[9] = cnt[6] | cnt[1];
    cnt[10] = cnt[2] & cnt[7];
    cnt[11] = cnt[4] | cnt[3];

    for (int i=0;i<11;i++)
        for (int j=i;j<=11;j++) {
            curr = cnt[i];
            if (cnt[j] == curr)
                bn++;
            if (bn>chmp)
                chmp=bn;
        }

    return chmp;
}

int S_Rend::s_render(ifstream& in, ofstream& out) {

    int i, n;
    int t;
    int chk;
    in >> lambda;
    in >> size;
    in >> delta;
    in >> chk;
    t=(int&)beta;
    int bn=0;

    while (size-1>=bn) {
        t=s_nop((int&)t,0);
        cred.push_back(t);
        bn++;       
    }

    if (cred[bn-1]==chk)
        cout << "\nValidity Pass... Success!" << endl;
    else {
        printf("\nValidity Pass...Fail! %u != %u",cred[cred.size()-1],chk);
        return 1;
    }

    cout << "\nWriting to Buffer..." << endl;
    i=0;
    spec = lambda;
int f;
while (bn-1>=0) {
    alpha = (int&)cred[bn-1];
    f=count(alpha, spec);
    eta = (int&)f;
    spec ^= alpha ^ eta;
    btrace.push_back(f);
    cout << f << " ";
    bn--;
}

    cout << "One more second..\n";

    while (i<=bn-1) {
        delta = (int&)btrace[bn];
        out << (const char)(int&)delta;
        i++;
    }

    cout << "\nBuffer Written... Exiting..\n";
    in.close();
    out.close();
    printf("*");
    return 0;
}

2 个答案:

答案 0 :(得分:2)

此时:     while(bn){     alpha =(int&amp;)cred [bn];     f = count(alpha,spec);

bn == size and size == cred.size()所以你读取了矢量。假设信誉在开始时是空的

答案 1 :(得分:1)

最明显的问题是你从具有越界索引的向量中写入(和阅读):

std::vector

好吧,我们可以在那里停下来。

vector的大小不正确,因此无法容纳任何元素。最后一行假定push_back可以保存至少一个项目(项目0)。因此,这是未定义的行为。

通过调用vector::resizevector::insertvector::emplace_backint S_Rend::count(bitset<8> alpha, bitset<8> spec) { int bn; vector< bitset<8> > cnt(12); bitset<8> curr; int chmp; eta = (alpha & spec); theta = (alpha | spec); cnt[0] = eta & alpha; // <-- ok ... } 来访问元素之前,适当地调整向量的大小,或者通过发布一个对向量进行大小调整的构造函数来构造向量除了构建它。

std::vector description

由于您似乎想要12个项目,因此您可以使用12个项目构建它。

operator [ ]

此外,作为调试辅助工具,您可以使用std::vector::at代替at()来访问矢量项。使用std::out_of_bounds会立即抛出std::bitset异常,表明您正在访问带有无效索引的向量(而不是程序继续,就好没有错误)。

第二件看起来可疑的事情是你将std::bitset转换为int引用,反之亦然。正如您所观察到的,没有演员表,您会收到错误。使用C风格的转换来关闭编译器&#34;并不是一个好主意,因为您所做的只是绕过C ++在编译时所具有的类型安全机制。

如果编译器告诉你&#34;这不应该完成&#34;,然后你通过发布C风格的演员来覆盖它,准备在你的节目显示不稳定时遭受后果行为。

要在intstd::to_ulong之间正确转换,有一些方法,即{{1}}。

How to convert from bitset to an int