Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const volatile char string not printing properly

I have a very simple program which just outputs indefinitely a const pointer pointing to a const volatile char; it goes like this:

const volatile char* const str = "ABCDEFGHIJKL";

while(true) {
    cout << '\r' << str;
}

The problem is that when running this program, the output is 1. There is a way to get around this, which is outputting const_cast<char*>(str) instead of str.

But if I do const_cast<volatile char*>(str) the output is 1, just like before the cast, so I'm guessing the 1 output is caused by the volatile keyword, which is strange because I thought that volatile only makes the compiler avoid optimizations in that variable, which shouldn't change the value of it.

My question, therefore, is how in the world did that 1 came as the output.

NOTE:

I've tried compiling it with GCC in Ubuntu 16.04, and with MinGW in Windows 7, so the compiler is not the problem(I guess).

like image 596
Garmekain Avatar asked Jun 28 '17 21:06

Garmekain


People also ask

Can const and volatile be used together?

Yes a C++ variable be both const and volatile. It is used in situations like a read-only hardware register, or an output of another thread. Volatile means it may be changed by something external to the current thread and Const means that you do not write to it (in that program that is using the const declaration).

What is the difference between const and volatile?

The const keyword specifies that the pointer cannot be modified after initialization; the pointer is protected from modification thereafter. The volatile keyword specifies that the value associated with the name that follows can be modified by actions other than those in the user application.

What is the effect of the variable declaring as both const and volatile?

We use 'const' keyword for a variable when we don't want to the program to change it. Whereas when we declare a variable 'const volatile' we are telling the program not to change it and the compiler that this variable can be changed unexpectedly from input coming from the outside world.

Is const opposite of volatile?

Let us get one thing straight: the concepts of const and volatile are completely independent. A common misconception is to imagine that somehow const is the opposite of volatile and vice versa. They are unrelated and you should remember the fact.


1 Answers

You got nuked by Implicit conversion Sequences (ICS). The C++ std::ostream facilities have no overloads for volatile types. ICS kicks in, and selects the overload for bool type (because pointer types, irrespective of cv qualifications are implicitly convertible to bool).

Hence you see 1... Change your output to std::boolalpha and you should see true instead.

Example:

#include <iostream>
#include <iomanip>

int main(){
    const volatile char* const str = "ABCDEFGHIJKL";
    std::cout << '\r' << str;
    std::cout << '\r' << std::boolalpha << str;
}

Prints:

1
true

Demo

like image 53
WhiZTiM Avatar answered Oct 19 '22 19:10

WhiZTiM