+ 6

Pointer to constant

There is code in c++: const double euler=2.72; double *peuler=(double*)&euler; cout<<&euler<<"="<<peuler<<endl; cout<<euler<<"="<<*peuler<<endl; *peuler=3.14; cout<<&euler<<"="<<peuler<<endl; cout<<euler<<"!="<<*peuler<<endl; I have const variable euler. This variable has address &euler. The pointer peuler points to variable euler. I change the value that is contained in the address peuler. And after it addresses &euler and peuler are equal, but values euler and *peuler are not equal. Why? https://code.sololearn.com/c82LT5S2fp4u/?ref=app

1st Jun 2018, 7:00 AM
Ivan Vladimirov
Ivan Vladimirov - avatar
7 ответов
+ 4
did not read on when i saw the mistake. Its an optimization problem you are noticing (but once again its actually you who is causing the trouble...). your compiler sees the const double euler, and assumes that it can resolve all occurrences at compile time, so it "replaces" euler with 2.72 at compile time for cout operator <<. on the other hand you change the value of the location to 3.14, but as the compiler replaces the 'euler' you pass to cout at compile time you notice 2 different values. a debugger told me that, its always clever to ask a debugger first ;). If you dont know how to use debugger you can use keyword volatile for euler and peuler so the compiler wont optimize and you can prove it for yourself.
1st Jun 2018, 4:12 PM
---
--- - avatar
+ 8
By doing: const double euler = 2.718282; double *peuler = (double*)&euler; You are casting the address of euler to non-const double*. While it is able to compile without errors, attempting to modify euler via peuler results in undefined behaviour. https://stackoverflow.com/questions/7311041/newbie-question-c-const-to-non-const-conversion
1st Jun 2018, 7:40 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
undefined behaviour, you can not cast const away !
1st Jun 2018, 7:36 AM
---
--- - avatar
+ 3
But how is that possible? One memory adress contains two different values? What is really at that place in memory?
1st Jun 2018, 1:14 PM
michal
+ 2
In a few words the definition of a pointer is quite simple: a pointer is a variable that contains a memory address [1]. This setting is simple, but the correct understanding of the use of this feature begins with an understanding of how the memory of a program is organized, or what a memory address is. Roughly, a memory can be defined as a set of elements that store information. These elements are called words, each word being uniquely identified from an address, that is, a memory address! A characteristic of the word is its ability to store information, that is, the amount of bytes that the word represents. Now we know that a memory is composed of words and that every word has a unique address, so it is important to know that a program is a set of information stored in memory, so the next step is to understand how a program is organized in memory. This topic is important to know what happens when a C program is compiled. A program is a set of information stored in memory, so we can divide a program into two categories: instructions and data. The instructions are the operations (the program itself) performed by the machine, and the data are the variables, or values, processed in those operations. Although the program consists of instructions and data, there are, conceptually, four memory regions Stack - The Stack is a dynamic region, that is, it varies according to the execution of the program. It is used to store the address in function and interrupt calls, pass parameters to functions, store local variables or can be manipulated to store data in a given operation; Heap - The Heap region is also dynamic, but differs from the heap. This region is considered free and is used by mechanisms of dynamic memory allocation; Data - The data region corresponds to the area where global and static variables are stored; Program - This region stores the program instructions.
2nd Jun 2018, 11:48 AM
Juan Carlos
Juan Carlos - avatar
4th Jun 2018, 11:44 PM
pubudi kaushalya
0
michal, I have the same question. Hartsy Rei and Ozren Zaric tried to answer to this question.
1st Jun 2018, 2:21 PM
Ivan Vladimirov
Ivan Vladimirov - avatar