Const variable in c ++

This is some kind of stupid question .. I want to ask .. Please help me figure this out.

const int i=100;   //1
///some code
long add=(long)&i;  //2

Doubt: for the above code ... the compiler first looks at all the code
to decide whether to allocate memory or not ... or first it will store the file variable in read-only memory, and then .. force the sequence out also by 2

doubt: why accept the address of a forced compiler variable to store the variable in memory. although rom or register also have an address

+3
source share
5 answers

add , , i. , , , , i , / . .

const , . ( ), . const , , .

const , - . , . , , , .

+5

, , , , , , , .

, , , , .

+1

( const, ) ( ROM, ). (100 ) , .

, , .

const " ", , " const", ( ) . , , .

, C/++ ( !), , , .

: , , .

+1
source

I am a little confused by what you ask, but looking at your code:

i = 100 with address 0x ????????????

add = no matter what address is stored as long int

0
source

There is no (dynamic) memory allocation in this code. Two local variables are created on the stack. The address I am taken and brutally inserted into the long, which is then assigned to the second variable.

0
source

Source: https://habr.com/ru/post/1792078/


All Articles