简体   繁体   中英

strange behavior in cout c++

Intro: In cout I expect any values passed by the insertion operator << to be displayed on screen.

Normally one would think that the following code would work without fault:

int n = 0;
cout << n;

And it does, and yes, it is good practice to always use endl . But the problem I am having is very strange (to me at least).

The Problem: I have the following code:

cout << " value at array point 0: "  << (list)[length -1] << endl;
cout << " Retry: " << (list)[length - 1];

list is a pointer pointing towards the 0 index memory location of an array of integers. length is the array length. You would imagine that this code would work without fault, correct? Wrong. For some reason the second line will not display - and I hadn't the slightest clue why. Then I added endl to the end of " Retry: " out of curiosity, and it worked. I don't know why, and it is really bothering me.

Thanks in advance to all help!



Basic overview of code

// Prototype
void listAdd( int* list, int& length );

int main()
{
   /* this program was created for practice with dynamic memmory with arrays.
   It should be able to extend the list, destroy it, and pop from it.
   */
    int length = 0;
    int *list = NULL;

    for( int i = 0; i < 5; i ++)
    {
        listAdd( list, length );
        //listDisplay( list, length);
    }

    cout << " if this has been displayed the program ended correctly." << endl;
    return 0;
}


void listAdd(int *list, int &length) {

    int* tempList = new int[ length + 1 ];
    for( int i = 0; i < length; i ++ )
    {
        (tempList)[i] = (list)[ i ];
    }


    cout << " Enter a number: ";
    int stored = 0;
    cin >> stored;

    cout << endl;
    if ( list != NULL )
        delete[] list;

    cout << " Previous adress: " << hex << list << endl;
    list = tempList;
    cout << " New address: " << hex << list << endl << dec;

    length ++;
    cout << " Length: " << length << endl;
    (list)[length -1] = stored;
    cout << " value at array point 0: "  << (list)[length -1] << endl;
    cout << " Retry: " << (list)[length - 1];
}

You need to flush stream manually. You should use "cout.flush()" after the code you posted.

What "std::cout << std::endl" does is - it prints '\\n' symbol and then it flushes the stream (same as std::cout.flush() .

PS it's not correct that using "endl" is always a good practice . You should usually use '\\n' for printing new line symbols. And use std::endl only in cases like these.

Streamed output is written to a buffer, and may not be written to the final destination until the stream is flushed. std::endl will insert an end-of-line, and then flush. You can insert std::flush , or call the flush() member function, to flush without inserting an end-of-line, if that's what you want.

it is good practice to always use "endl"

Not really. Flushing too often, especially when writing to a file, can degrade performance.

endl flushes the stdout stream. Your output is probably just in the buffer waiting to be printed to the screen. There is nothing wrong with the behavior (besides that you did expect something else).

Try a manual flush instead of endl. That should result in the same behavior.

Quoting cppreference :

Inserts a endline character into the output sequence os and flushes it as if by calling os.put(os.widen('\\n')) followed by os.flush().

So the output is just in the buffer. Outputting more stuff - or explicitly flushing - will cause it to be displayed.

I agree with other answers stating that unnecessarily adding std::endl or more generally flushing more often than you need to is not a good practice.

If you added an std::endl to the end of the second output and it worked, it is because endl flushes the stream buffer. Otherwise, you are waiting for the next buffer write to the screen (and if your program exits before that happens, you likely don't see it before it closes the window - it would write it out and close the program almost instantly).

You need to flush the stream if you have not caused that to happen automatically.

Sending a newline (endl) will cause the stream to flush.

If you really want to flush and keep the cursor on the same line, add cout.flush() to explicitly flush the buffered stream data. eg:

cout << " value at array point 0: "  << (list)[length -1] << endl;
cout << " Retry: " << (list)[length - 1];
cout.flush();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM