Why do I get this strange output behavior?

Posted by WilliamKF on Stack Overflow See other posts from Stack Overflow or by WilliamKF
Published on 2010-05-22T17:33:53Z Indexed on 2010/05/22 17:40 UTC
Read the original article Hit count: 206

I have the following program test.cc:

#include <iostream>
unsigned char bogus1[] = {
  // Changing # of periods (0x2e) changes output after periods.
  0x2e, 0x2e, 0x2e, 0x2e
};
unsigned int bogus2 = 1816; // Changing this value changes output.

int main()
{
  std::clog << bogus1;
}

I build it with:

g++ -g -c -o test.o test.cc; g++ -static-libgcc -o test test.o

Using g++ version 3.4.6

I run it through valgrind and nothing is reported wrong.

However the output has two extra control characters and looks like this:

....

Thats a control-X and a control-G at the end.

If you change the value of bogus2 you get different control characters. If you change the number of periods in the array the issue goes away or changes.

I suspect it is a memory corruption bug in the compiler or iostream package.

What is going on here?

© Stack Overflow or respective owner

Related posts about c++

Related posts about iostream