-1

Forethought: If it is needed then I can add the class definition. Problem: I get a STATUS_ACCESS_VIOLATION whenever I try to run this function in my program. I was wondering what was going on. Am I out of bounds somewhere? If I could reason it out myself I would. But I cannot figure it out alone. I'm very close to just hiring someone to do the debugging for me. It's worth my wile. So anyway, this needs to be looked over and given a little TLC. Thanks in advance!

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;
}
thexiv
  • 27
  • 9
  • 2
    When you used a debugger, which statement(s) are causing the issue? What are the values of the variables used in the statement(s)? – Thomas Matthews Nov 28 '15 at 06:07
  • What are you supposed to be doing with lines like this? `delta = (int&)btrace[bn];`? Why are you casting to an int reference? – PaulMcKenzie Nov 28 '15 at 06:14
  • Please don't repost the same question again and again. Also, [tag:data-mining] is *not* bitcoin mining, obviously. – Has QUIT--Anony-Mousse Nov 28 '15 at 12:22
  • You really should hire someone to fix your code then. – Has QUIT--Anony-Mousse Nov 28 '15 at 12:24
  • @PaulMcKenzie I'm casting those to bitsets. 8 bits per. So they need to be formatted just such the way I have them after casting. I tried without and got compilation problems. `delta` is a `bitset<8>` – thexiv Nov 28 '15 at 15:17
  • @ThomasMatthews My cygwin doesn't come with the libpython2.7.dll so I can't debug with gdb. REALLY sad that I can't. I'm very upset about that. But I bought VS 2010. And hopefully that'll help get the problems over with. – thexiv Nov 28 '15 at 15:20
  • Cygwin does not install python in the default load. Run the cygwin setup program. When you get to Package selection, expand the python package group and click where it says **skip** in front of the **Python: Python Language Interpreter** package. skip should turn into the version number that's about to be installed. Next your way through and let it install. – user4581301 Nov 28 '15 at 16:01
  • AH!! THANK YOU!! @user4581301 You sir are a scholar and a gentleman. – thexiv Nov 28 '15 at 16:07
  • @user4581301 Is this an `overflow` problem? I'm doing a lot of work in it. Is it too much for my computer? The problem comes right after hitting the other function `count()`. – thexiv Nov 28 '15 at 16:57
  • @thexiv Remove the casts. What compiler error do you receive? Whatever it is, read it carefully -- the compiler is detecting that something stinks, and you're trying to hide the stink by casting. – PaulMcKenzie Nov 28 '15 at 17:11
  • `vector< bitset<8> > cnt;` does not specify a size of the vector, nor do you resize to reserve any space or push_back elements. When you start indexing it, here for example: `cnt[0] = eta & alpha;` Crom only knows what will happen or where in memory the assignment will go, if it gets that far. Since you seem to know you want 12 elements in the vector, construct it with `vector< bitset<8> > cnt(12);` Read up on vectors here: http://en.cppreference.com/w/cpp/container/vector – user4581301 Nov 28 '15 at 17:22
  • @user4581301 Thank you so much. That did the trick. Would you know if I can recursively go through the byte I have and, allowing that I'm going the other way, be able to take out the alpha and the spec from f, which is my plan? – thexiv Nov 28 '15 at 18:00

2 Answers2

2

At this point : while (bn) { alpha = (int&)cred[bn]; f=count(alpha, spec);

bn == size and size == cred.size() so you read ouside the vector. Assuming cred is empty at start

Jerome
  • 529
  • 4
  • 14
1

The most glaring issue is that you're writing (and reading) from a vector with out-of-bounds indices:

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;  // <-- Illegal access
    ...
}

Well, we can stop right there.

The std::vector is not sized, so it cannot hold any elements. The last line assumes that vector can hold at least one item (item 0). Thus this is undefined behavior.

Size the vector appropriately before accessing elements by calling push_back, vector::resize, vector::insert, vector::emplace_back, or construct the vector by issuing one of the constructors that sizes the vector in addition to constructing it.

std::vector description

Since it seems you want 12 items, then you can construct it with 12 items.

int 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
    ...
}

Also, as a debugging aid, you could have used std::vector::at instead of operator [ ] to access the vector items. Using at() would have immediately thrown a std::out_of_bounds exception indicating that you were accessing the vector with an invalid index (instead of the program continuing as if nothing is wrong).


The second thing that seems dubious is you casting a std::bitset to an int reference, or vice-versa. As you observed, without the cast, you get an error. Using a C-style cast to "shut the compiler up" is not a good idea, as all you're doing is bypassing the type-safety mechanism that C++ has in place at compile-time.

If the compiler says to you "this shouldn't be done", and then you override it by issuing a C-style cast, prepare to suffer the consequences if / when your program shows erratic behavior.

To properly convert between a std::bitset and an int, there are methods for this, namely std::to_ulong.

How to convert from bitset to an int

Community
  • 1
  • 1
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • I fixed all that and she's running like a gallupin horse, hyuk. But I wonder, if I just used a `&` encryption, how would I get that back with a key? I can't find the XOR encryption destroyer. – thexiv Nov 29 '15 at 00:11