-1
int main()
{
volatile int a=26;
volatile int *p=&a;
cout<<p;  

return 0;
}
  1. Output is 26 in Borland compiler, in gcc O/P is 1. Why?

  2. What happens if many variable or a large object is declared as register, will compiler automatically disallow it to load on the registers ,instead load them on the memory, or hang, show error, etc.?

THANKS :)

Tuesday
  • 55
  • 1
  • 7
  • Are you thinking `volatile` means "put in register?" – YSC Dec 18 '15 at 09:51
  • 4
    Borland compiler is ancient and broken. In gcc, it is displayed as a bool for this reason: http://stackoverflow.com/questions/2501737/why-does-stdcout-convert-volatile-pointers-to-bool/2501791#2501791 – interjay Dec 18 '15 at 09:51
  • volatile means that the compiler will not optimise the code by storing the variables on the registers. Declaring volatile means for sure the variable will be stored in the memory, I know that, an in the memory we don't have address like 1 or 26 – Tuesday Dec 18 '15 at 09:54
  • In this case, I can't understand your question "What happens if many variable or a large object is declared as register...?" – YSC Dec 18 '15 at 10:03
  • `volatile int *p=&a;` make `p` a pointer to `a`, storing `a`'s address in memory. That's got nothing to do with the contents of `a`'s memory, i.e. `26`. If you cast it `(void*)p` you'll see the memory address. Without the cast, GCC uses a bizarre `operator<<(std::ostream&, T)` overload; likely `T` is `bool` as others have claimed and the output is effectively meaningless: it just means the pointer wasn't `nullptr`. Borland outputting `26` is totally broken and inexplicable as interjay says. – Tony Delroy Dec 18 '15 at 10:04
  • You have already been answered, that it converts pointer to bool. This is what happens. You can inspect resulting code with debugger to see which overload was chosen. I want to add, that standard library is usually unsufficiently prepared to handle volatile objects. – Revolver_Ocelot Dec 18 '15 at 10:04
  • Regarding 2, have a read of [drdobbs](http://www.drdobbs.com/keywords-that-arent-or-comments-by-anoth/184403859) - it explains that register is just a hint to the compiler; if you use it too many times, the compiler simply picks some uses to arbitrarily ignore - using memory instead. – Tony Delroy Dec 18 '15 at 10:10
  • I think most modern compilers will simply ignore `register`. They are *so* much better than humans at optimizing register usage. – Martin Bonner supports Monica Dec 18 '15 at 10:29

1 Answers1

4
  1. There is no overload for operator<<(volatile int*) in std::ostream, so implicit type conversions are considered. volatile pointers aren't implicitly convertible to non-volatile, so std::ostream::operator<<(void*) - which would typically be used in case of pointers - is not an option. There is however, an overload for a type that even a volatile pointer can be converted to: operator<<(bool). Since the pointer is not null, it's converted value is true, which cout outputs as 1. If you want to print the address, then const_cast the volatile qualifier away so that the implicit conversion to void* is used. If you want to print the pointed value, then pass the pointed value instead of the pointer by dereferencing it.

    Borland appears to provide a non-standard overload that dereferences the pointer.

  2. There is no way to declare a variable as a register in C++. Yes, there is a register keyword, but it does not change the meaning of the program and has been deprecated (and is planned to be removed in c++17 version of the standard). It's entirely up to the compiler to decide if a variable is stored in memory or merely in a register.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326