There is a code

#include <iostream> using namespace std; int main() { bool *a = new bool(true); cout << a << endl; cout << *a << endl; cout << &a << endl; } 

And he outputs

 0x4414f10 1 0x71d2e85b4548 

But what do the first and third numbers mean? Which one is the address?

  • The 1st line shows the address where the bool variable is stored; The 2nd line shows the value by address (i.e. the value of the variable); The 3rd line shows the address where the address is stored, where the bool variable is stored (called the pointer to the pointer). From 1 and 3 both are addresses, only 1 indicates a variable, and 3 indicates 1 - goldstar_labs

2 answers 2

The first number is the address of a bool variable created with new .
The second is the value of this variable.
The third is the address of the variable in which the first value is stored.

  • What does the first value mean? - ishidex2
  • And how come they are of different dimensions? a is 32 bit and & a is 64 or is it a consequence of running on vm? - goldstar_labs
  • one
    They have one dimension, only the memory for "true" was allocated in the heap, and the variable "a" was created on the stack, therefore the addresses are so different - ffk
  • @ffk, it turns out that cout just cut insignificant zeros at the beginning, do I understand correctly? - goldstar_labs pm
  • @goldstar_labs right - ffk 2:57 pm

It is possible with comments:

 bool *a = new bool(true); // Π·Π°Π½ΠΈΠΌΠ°Π΅ΠΌ ΠΏΠ°ΠΌΡΡ‚ΡŒ Π² Ρ€Π°Π·ΠΌΠ΅Ρ€Π΅ sizeof(bool*) с ΠΈΠΌΠ΅Π½Π΅ΠΌ 'a', Π³Π΄Π΅ записываСм //адрСс ΠΎΠ±ΡŒΠ΅ΠΊΡ‚Π°, созданная Π² динамичСской памяти // ΠΈ занятая Π½Π°ΠΌΠΈ ΠΏΠ°ΠΌΡΡ‚ΡŒ для ΡƒΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒΡ ΠΈΠΌΠ΅Π΅Ρ‚ адрСс cout << "адрСс Π² динамичСской памяти _ " << a << endl; cout << "Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΎΠ±ΡŒΠ΅ΠΊΡ‚Π° _ " << *a << endl; cout << "адрСс Π² локальной памяти _ " << &a << endl; 

pointer occupies memory in one machine word. This is 4 bytes or 8, depending on the machine.