Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are const variables placed in read-only memory? [duplicate]

Tags:

c++

c

constants

Or is there some other protection against modifying them?

It makes sense if they are in read-only memory - that's the reason for making them const, right?

like image 596
Oleksiy Avatar asked Aug 05 '13 12:08

Oleksiy


4 Answers

const is a compile time construct and isn't known about at runtime. It is only there to help the programmer reason about his program and to prevent bugs being introduced by altering things that were not supposed to be altered. const tells the compiler that you don't want to allow this variable to be changed, and so the compiler will enforce it.

like image 144
Tony The Lion Avatar answered Oct 20 '22 00:10

Tony The Lion


No they are not required to be. const is compile time, and allow compiler to perform some kind of optimization. However, it is not mandatory that a variable be placed into a read-only memory location.

See this example, which is Undefined Behaviour (Thanks to Dyp for pointing that out):

#include <iostream>

int     main()
{
  const int bla = 42;
  int       *ptr = const_cast<int *>(&bla);

  std::cout << *ptr << std::endl;
  *ptr = 21;
  std::cout << *ptr << std::endl;
}

It will output 42 and 21, but it could also crash.

Now see this one:

#include <iostream>

int     main()
{
  const int bla = 42;
  int       *ptr = const_cast<int *>(&bla);

  std::cout << bla << std::endl;
  *ptr = 21;
  std::cout << bla << std::endl;
}

On my compiler, this output 42 and 42, because the compiler made some optimizations. Note that it could still crash because of *ptr = 21;

like image 27
Xaqq Avatar answered Oct 20 '22 00:10

Xaqq


There are a lot of cases where it's not possible for the compiler to make a const into read-only memory (assuming there is read-only memory in the system in the first place). In fact, I believe nearly all compilers, as a rule, makes const objects live in regular data (or stack) memory, just like ordinary variables.

The main purpose of const is to declare your intent with the compiler that you do not want to and aren't supposed to change some value. I don't see any reason why compilers can't, under limited circumstances, put const variables in read-only memory. But I also wouldn't rely on this - the standard certainly makes this possible, since it mentions that using const_cast remove const from an object that was originally marked const, and then writing to it is undefined behaviour (so, it doesn't require the compiler to allow the value to be modified after using const_cast to remove the original const - and thus does allow for "Crash because we tried to write to read-only memory").

But consider this:

class X
{
   int x;
  public:
   X(int v) : x(v) {}
}

int c = rand();

const X a(c+1);
const X b(c+2); 

In this case, the compiler can't know the value in c that it got from rand, so it can't initialize a and b at compile time.

like image 6
Mats Petersson Avatar answered Oct 20 '22 00:10

Mats Petersson


The const keyword has two uses:

  1. The well-known "I promise not to modify this object's state"
  2. As a special rule, the compiler may place objects whose definition is const in read-only memory as long as the object has no mutable members (i.e. the entire object is the object's state).

That is why const_cast ist allowed to strip away the const qualifier, but a requirement is that the object definition itself is not const. Wikipedia on const correctness states that "However, any attempt to modify an object that is itself declared const by means of const_cast results in undefined behavior according to the ISO C++ Standard.".

like image 4
Simon Richter Avatar answered Oct 20 '22 00:10

Simon Richter