473,388 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

[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.
Expand|Select|Wrap|Line Numbers
  1. void dump() {
  2.     char value[8];
  3.     for(int a = 1; a < 11; a++) {
  4.         cout << "r" << a << ": \t";
  5.         sprintf(value,"%08X", r[a]);
  6.         for(int b=0; b<8;b++) {
  7.             cout << value[b];
  8.             if((b+1)%2==0)
  9.                 cout << " ";
  10.         }
  11.         if(a%2==0)
  12.             cout << "\n";
  13.         else
  14.             cout << "\t";
  15.     }
  16.     return;
  17. }
Dec 6 '08 #1
4 1457
Ganon11
3,652 Expert 2GB
Your code doesn't loop for me...

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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int r[12];
  5.  
  6. void dump() {
  7.     char value[8];
  8.     for(int a = 1; a < 11; a++) {
  9.         cout << "r" << a << ": \t";
  10.         sprintf(value,"%08X", r[a]);
  11.         for(int b=0; b<8;b++) {
  12.             cout << value[b];
  13.             if((b+1)%2==0)
  14.                 cout << " ";
  15.         }
  16.         if(a%2==0)
  17.             cout << "\n";
  18.         else
  19.             cout << "\t";
  20.     }
  21.     return;
  22. }
  23.  
  24. int main() {
  25.     for (int i = 0; i < 12; i++)
  26.         r[i] = i;
  27.     dump();
  28.     system("PAUSE");
  29.     return 0;
  30. }
It produces this output:

Expand|Select|Wrap|Line Numbers
  1. r1:     00 00 00 01     r2:     00 00 00 02
  2. r3:     00 00 00 03     r4:     00 00 00 04
  3. r5:     00 00 00 05     r6:     00 00 00 06
  4. r7:     00 00 00 07     r8:     00 00 00 08
  5. r9:     00 00 00 09     r10:    00 00 00 0A
  6. 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.
Dec 6 '08 #2
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
Dec 6 '08 #3
Banfa
9,065 Expert Mod 8TB
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.
Dec 7 '08 #4
Thx for the help fixed my problem. I <3 you.
Dec 7 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
13
by: Bev in TX | last post by:
We are using Visual Studio .NET 2003. When using that compiler, the following example code goes into an endless loop in the "while" loop when the /Og optimization option is used: #include...
1
by: SJ | last post by:
I'm developing a WAP client which presently works fine on most mobile phone browsers, but gives me an Infinite loop error (error 1025) when i try to access it from a couple of phones(motorola for...
59
by: rami | last post by:
please everybody ,can anyone tell me how to do an infinite loop in C
13
by: Vector | last post by:
Is any infinite loop better than other? Is there any difference between there efficiency?
6
by: Luke - eat.lemons | last post by:
Hi, Im pretty new to asp so all light on this question would be great. Basically i need to test to see what value is set (where to retrieve the data from) so ive done it like this: If...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
10
by: =?ISO-8859-1?Q?G=E9rard_Talbot?= | last post by:
www.authoring.stylesheets] Dear fellow CSS colleagues and web authors in alt.html discussion forum, I would like to ask you to help me confirm that there is a serious bug in IE 7 final release...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.