C++ won’t print, even with cout::flush

Why doesn’t std::cout print?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

using namespace std;

    int disassemble8080(int programCounter, unsigned char* codeBuffer)
{
    short OPsize = 1;
    switch(codeBuffer[programCounter])
    {
    case 0x00:
        cout << "NOP\n"; 
        break;
    case 0x01:
        cout << "LXI B " << codeBuffer[programCounter + 0x02]
        << codeBuffer[programCounter + 0x01];
        OPsize = 3;
        break;
        
    }
    return OPsize;
}

int main()
{
    unsigned char myCodes[10]= {1, 34, 12};
    disassemble8080(0, myCodes);
    cout.flush();
    return 0;
}

This doesn’t print anything, even after cout::flush
Last edited on
Works for me, prints
LXI B "
(junk and double-quote included, since you're printing characters outside of normal ascii range, e.g. ascii 12).

cout.flush is not needed, it will be flushed when program ends if not before.

Try cleaning your project, or making a new one and pasting the code in.
Last edited on
some consoles may print nothing you can see for unprintables.
try redirect the output to a file to see if you got bytes there. Or print the hex or decimal instead of the ascii for it. But even so you should have seen the LXI part of it.
Last edited on
@jonnin @Ganado Ii found the solution. so basically the unsigned char was being interpreted as the corresponding ascii code when I wanted it to be an integer. I fixed this by casting the print to print the integer version of the unsigned char. I am worried that the cast will increase the size of the 1 byte integer, because the system that I am emulating only works with unsigned char.
As long as you're only casting it when printing, it won't be an issue (won't affect any storage of the chars).
Last edited on
it unfortunately promotes it inside the code, so when it loads the CPU register, it takes up extra space there, which could impact performance if your CPU allows partial register use (eg al and ah as one byte registers sharing the A register in intel) AND if your code was actually USING the partial registers for something at that moment in time. This is a pretty small impact, because writing text is ABYSMALLY slow -- you convert 1 byte into 2(max hex) or 3 (max integer) to print it as text, and that process is sluggish because there is a bit of work that needs to be done to covert into textual format for numbers. (there are hand-rolled c++ codes that do this job far, far better than the built in tools, interestingly!). Because conversion to text is so slow already, using the whole register instead of only 1 byte of it 1) does no real harm to the real space your program uses, as noted above ^^ and 2) does no real performance harm because the slowness of value to text overwhelms anything that the loss of the partial register was gaining for you. It is also possible that the value to text does more work than necessary because the # of bytes is oversized and it has to check them all, see that they are zeros, and not print anything for the leading zero bytes of the integer. Joy.

I recently did this for some high performance code to convert value to text...
static const uint32_t k1[] = //0000 to 0999 ascii strings as integers.
{
808464432, 825241648, 842018864, 858796080, 875573296, 892350512, 909127728, 925904944, 942682160, 959459376,
808529968, 825307184, 842084400, 858861616, 875638832, 892416048, 909193264, 925970480, 942747696, 959524912,
808595504, 825372720, 842149936, 858927152, 875704368, 892481584, 909258800, 926036016, 942813232, 959590448, ... 1000 of these suckers.



if this annoys you, since its bytes, you can just make a lookup table of the text you need to print if you want it faster and tighter, and skip the cast. If you need to do a multi-byte int that way, its a little more involved. You can write a program to generate the table. At the risk of getting everyone worked up, this kind of thing is going to go faster in c-strings. Its one of those 'do you really, really, really need that performance boost this badly' things. I would not recommend it generally.

TLDR... roll your own value to ascii and feed that into cout if you are really looking to squeeze cpu cycles. And, for the OP, don't worry about it, its fine as you have it.
Last edited on
Topic archived. No new replies allowed.