I cannot relate this statement to the talk about ROM and hardware from
before. If you just want to indicate that the compiler is free to put a
piece of data into a read-only segment of memory, you can already use
const.
-1-
Better to say, that compiler is ignoring existance such of special and
useful memory areas, as some kind of readonly/writeonly memory. The using
unqualified C-style pointers can lead to runtime errors, when pointers
from readonly segments will be mixed with ordinary memory.
I am repeating, that to avoid the possible errors:
1. we can refuse using C-style pointers without explicit memory qualifiers
and new/new[]
2. or new/new[] must return user defined classes instead of C-style
pointers 3. or we must invent user-defined operators instead of new/new[]
to create dynamic data
-2-
Maybe explicit memory specifiers can be useful as itself (without
concerning dynamic data problems), they do not disturb systems allocating
only one segment, but allow to other systems manualy place data into
concrete segments, "switching on" hardware memory control.
Let you have (it is real sysytem)
- code1 code segment as can not rw
- code2 code segment as readonly
- data1 data segment as readonly
- data2 data segment as rw
- data3 data segment as writeonly (for your process)
where "Tobj" and "ptr" will be placed in following example?
const Tobj *const ptr;
const Tobj * ptr;
Tobj *const ptr;
Tobj * ptr;
but with explicit memory specification
default segments:
data1 - for const T
data2 - for T
// ! not C++
//Tobj -> code2
//ptr -> data2
code Tobj *ptr; //error code2 is const
code const Tobj *ptr;
//Tobj -> data1
//ptr -> data2
const Tobj *ptr;
const_cast<Tobj&>(ptr); //error - data1 readonly
//or compiler will remove Tobj into data2 and print warning
//or compiler will make temporary and print warning
//Tobj -> data2
//ptr -> data2
writable const Tobj *ptr; //software constness
const_cast<Tobj&>(ptr); //ok
//Tobj -> data2
//ptr -> data2
const Tobj dseg * ptr; //software constness for Tobj
//Tobj -> data2
//ptr -> data1
Tobj *const ptr;
//Tobj -> data2
//ptr -> data2
Tobj *const writable ptr; //software constness for ptr
//Tobj -> data2
//ptr -> code2
Tobj *code ptr; //error - code is
readonly
Tobj *const code writeable ptr; //error - no writeable code seg
Tobj *const code ptr;
We need new keywords "writeable, writeonly, readable, readonly, dseg,
code, heap, heap[] "