[c++] code seems to be in infinite loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dschu012
    New Member
    • Jul 2008
    • 39

    [c++] code seems to be in infinite loop

    Can anyone help explain to me why "a" doesn't seem to be incrementing here? And constantly couting "r1 = 00 00 00 00" then a new line.
    Code:
    void dump() {
    	char value[8];
    	for(int a = 1; a < 11; a++) {
    		cout << "r" << a << ": \t";
    		sprintf(value,"%08X", r[a]);
    		for(int b=0; b<8;b++) {
    			cout << value[b];
    			if((b+1)%2==0)
    				cout << " ";
    		}
    		if(a%2==0)
    			cout << "\n";
    		else
    			cout << "\t";
    	}
    	return;
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Your code doesn't loop for me...

    The following is test code I created to supplement your function:

    Code:
    #include <iostream>
    using namespace std;
    
    int r[12];
    
    void dump() {
        char value[8];
        for(int a = 1; a < 11; a++) {
            cout << "r" << a << ": \t";
            sprintf(value,"%08X", r[a]);
            for(int b=0; b<8;b++) {
                cout << value[b];
                if((b+1)%2==0)
                    cout << " ";
            }
            if(a%2==0)
                cout << "\n";
            else
                cout << "\t";
        }
        return;
    }
    
    int main() {
    	for (int i = 0; i < 12; i++)
    		r[i] = i;
    	dump();
    	system("PAUSE");
    	return 0;
    }
    It produces this output:

    Code:
    r1:     00 00 00 01     r2:     00 00 00 02
    r3:     00 00 00 03     r4:     00 00 00 04
    r5:     00 00 00 05     r6:     00 00 00 06
    r7:     00 00 00 07     r8:     00 00 00 08
    r9:     00 00 00 09     r10:    00 00 00 0A
    Press any key to continue . . .
    The problem might be in how you've defined r - for instance, you start a at 1 and run it to 10, so if r is smaller than that your code might break.

    Comment

    • dschu012
      New Member
      • Jul 2008
      • 39

      #3
      I'm trying to run it on Knoppix, it was working fine when I was using Ubuntu but when I tried to compile and run it on Knoppix I encountered this problem

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        value is defined as

        char value[8];

        but you sprintf 9 characters into value via

        sprintf(value," %08X", r[a]);

        8 digits plus the terminator.

        You have exceed the array bounds means you have invoked undefined behaviour. As such whether it works on one system or another is irrelevant. On any single instance of execution anything could happen.

        My guess is something along the lines that your Ubuntu system runs on an Intel and is thus little endian. Your Knoppix system runs on a big endian processor. This means the zero terminator of the string when written over writes the most significant byte of a on Ubuntu. Since this is always 0 this has no effect. On the Knoppix system it over writes the least significant byte of a effectively zeroing it on every loop iteration.

        However that is irrelevant as I said undefined behaviour has been envoked anything could happen.

        Fix the undefined behaviour by declaring value as

        char value[9];

        and in future don't forget the zero terminators on C strings.

        Comment

        • dschu012
          New Member
          • Jul 2008
          • 39

          #5
          Thx for the help fixed my problem. I <3 you.

          Comment

          Working...