For example:
#include <stdio.h>
#include <string>
int main() {
std::string* stuff(NULL);
printf("allocating memory..."); //line 2
stuff = new std::string[500000000]; //line 3
delete [] stuff; //line 4
return 0;
}
when executed runs line 3 (and possibly line 4) before line 2. Now I know this is probably some good optimization feature but sometimes the right order is needed.
The problem is here:
In many architectures you have buffered output, which means that what you print on the screen is not shown immediately but stored in a memory buffer. To flush the buffer and ensure that he is printed immediately, you can use
although I didn’t find anything to prove this besides personal experience, or alternatively, if you don’t want to go to a new line (and be absolutely sure of flushing) you can use
fflush(stdout)right after line 2.