By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,526 Members | 1,895 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,526 IT Pros & Developers. It's quick & easy.

endless loop

P: n/a
This function that I have implemented gives me an infinite loop. I am
trying to produce a hexdum program, however, this function is not
functioning correctly.....Please help.

void output(unsigned char ret_buffer, int curr_buffer_size, bool&
endoffile)
{
int index2=0;
int addr=0;
unsigned char outBuff[curr_buffer_size];
outBuff[curr_buffer_size] = ret_buffer;

while (!endoffile)
{
cout<<setw(4)<<setfill('0')<<uppercase<<hex<<addr< <"0: "; //
prints mem location
for (int index = 0; index <16; index++)
{
index2++;
if (index2 <= curr_buffer_size)
cout<<hex<<setw(2)<<setfill('0')<<(int)outBuff[index];
else
cout<<" ";
cout<<" ";
}//end for
cout<<setfill(' ');
//cout<< " ");
index2 = 0;
for (int index = 0; index < 16; index ++)
{
index2++;
if (index2 <= curr_buffer_size)
{
if (outBuff[index] < 32 || outBuff[index] 126)
printf(".");
else
cout<<outBuff[index];
}//end if
}//end for
}
}

Feb 28 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 27 Feb 2007 19:52:56 -0800, "uche" <ur***********@hotmail.com>
wrote in comp.lang.c++:
This function that I have implemented gives me an infinite loop. I am
trying to produce a hexdum program, however, this function is not
functioning correctly.....Please help.

void output(unsigned char ret_buffer, int curr_buffer_size, bool&
endoffile)
{
int index2=0;
int addr=0;
unsigned char outBuff[curr_buffer_size];
The line above is not valid C++ code. In C++, the size of an array
must be a compile time constant. You are using some compiler's
non-standard extension.
outBuff[curr_buffer_size] = ret_buffer;
Even assuming that you are using a non-standard extension, the line I
just pointed out defines an array of curr_buffer_size unsigned chars.
Valid elements of that array are [0] through [curr_buffer_size - 1].
So the line just above is writing one past the end of the array.
while (!endoffile)
As somebody else already pointed out, "endoffile" is passed in as a
parameter and never changes. If it is non-zero when the function is
called, this while loop will run forever.
{
cout<<setw(4)<<setfill('0')<<uppercase<<hex<<addr< <"0: "; //
prints mem location
for (int index = 0; index <16; index++)
{
index2++;
if (index2 <= curr_buffer_size)
cout<<hex<<setw(2)<<setfill('0')<<(int)outBuff[index];
else
cout<<" ";
cout<<" ";
}//end for
cout<<setfill(' ');
//cout<< " ");
index2 = 0;
for (int index = 0; index < 16; index ++)
{
index2++;
if (index2 <= curr_buffer_size)
{
if (outBuff[index] < 32 || outBuff[index] 126)
printf(".");
else
cout<<outBuff[index];
}//end if
}//end for
}
}
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 28 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.