endless loop

Discussion in 'C++' started by uche, Feb 28, 2007.

  1. uche

    uche Guest

    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
    }
    }
    uche, Feb 28, 2007
    #1
    1. Advertising

  2. uche

    Jack Klein Guest

    On 27 Feb 2007 19:52:56 -0800, "uche" <>
    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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Feb 28, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmlsbCBCb3Jn?=

    Endless loop on delete auth cookie/abandon session

    =?Utf-8?B?QmlsbCBCb3Jn?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    660
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Feb 2, 2005
  2. Alexandre
    Replies:
    4
    Views:
    468
    Lasse Reichstein Nielsen
    Apr 12, 2005
  3. Leo
    Replies:
    1
    Views:
    404
    Thomas Heller
    Jul 4, 2003
  4. Claudio Grondi
    Replies:
    75
    Views:
    1,112
    Tim Peters
    Jan 21, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    349
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page