0

I wrote a piece of code and tested with gcc compiler

#include <iostream>

int main()
{
    char arr[ 1000 ];
    for( int index( 0 ); index < 1000; ++index )
    {
        std::cout << arr[ index ] << std::endl;
    }
    return 0;
}

I was hoping it to print the garbage values but to my surprise, it did not print anything. When I simply changed the datatype of arr from char to int, it displayed the garbage values as expected. Could somebody please explain this to me?

5 Answers 5

3

The overloads for << for character types do not treat them as integral types, but as characters. If the garbage value corresponds to a printable character (e.g. 97, which corresponds to 'a'), you will see it. If it doesn't (e.g. 0), you won't. And if the garbage values correspond to some escape sequence which causes your terminal to use a black foreground on a black background, you won't see anything else, period.

If you want to see the actual numerical values of a char (or any character type), just convert the variable to int before outputting it:

std::cout << static_cast<int>( arr[index] ) << std::endl;
Sign up to request clarification or add additional context in comments.

1 Comment

I got it. So basically what is happening over here is that the random value are not printable ascii character codes that is why I am not seeing anything. Thank you so much for pointing it out.
2

What you're trying to do has an undefined behavior. Some compilers will clear out the memory for you, others will leave it as it was before the creation of your buffer.

Overall, this is a useless test.

1 Comment

It's not really 'undefined behavior' in the sense that the C++ standard uses that term. All values of char are required to be valid and so it should be perfectly legal and well behaved to read indeterminate char values.
2

Some platforms may choose, for example for security purposes, to fill the uninitialized char array with zeroes, even though it's not static and wasn't explicitly initialized. Therefore, that is why no garbage is showing up - your char array was just automatically initialized.

Comments

1

On your platform garbage characters don't print. On another platform it might be different.

As an experiment try this

std::cout << '|' << arr[ index ] << '|' << std::endl;

See if anything appears between the || characters.

2 Comments

More likely the "garbage values" are 0, no?
@juanchopanza More likely, the garbage values are whatever happens to be left there from previous functions. The system will initialize any memory it gives the process to zero, for security reasons, but crt0 has probably called a few functions before calling main, and these will probably have left non-zero values here and there.
0

You're getting undefined behaviour because you're attempting to use values from an uninitialised array. You can't expect anything in particular to happen. Maybe every character happens to be a non-printing character. Maybe it just decided that it didn't want to print anything because it doesn't like your little games. Anything goes.

1 Comment

Actually, char is an exception, I think. (At least unsigned char is, and I think C++ extended this guarantee to char.) The standard does not allow char to have any trapping representation, and does allow using it to read "raw memory", including uninitialized raw memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.