Is there any real reason to use "const"?

Walter Bright newshound2 at digitalmars.com
Mon Jan 24 19:59:37 UTC 2022


On 1/24/2022 7:44 AM, Walter Bright wrote:
> On 1/24/2022 6:40 AM, Steven Schveighoffer wrote:
>> immutable/const is purely a compile-time concept. It's not reflected in the 
>> final binary, so it's not necessary to forward the attributes to a language 
>> that doesn't support it.
> 
> Immutable global data gets placed in read-only memory sections. Read-only memory 
> sections are nice in a demand-paged virtual system, as the pages they are in 
> never have to be copied because they are never marked as "dirty".

Also, immutable data can be merged together, reducing the memory footprint. The 
dmd compiler does this with strings, for example. Identical string literals are 
set up to get merged at link time, this can only happen if they are immutable.

Immutable zero initialized data also gets merged. A zero is a zero, no matter 
what type it is!


More information about the Digitalmars-d mailing list