In generaly memory can be readable and writable.when C compiler set memory const,what is mechan of it?who block the memory being written.if by mistake to force to write the marked const memory,who is reporting the segment error?
-
1That's part of the OS and/or underlying hardware. And trying to modify "const memory" is usually *undefined behavior*, there's no guarantee of a "segment error" – UnholySheep May 14 '18 at 07:55
-
the segments of a program can be read-only or writable. The operating system places them in different pages – Jean-François Fabre May 14 '18 at 07:55
-
1https://en.wikipedia.org/wiki/Memory_management_unit – mch May 14 '18 at 07:57
3 Answers
There is no specified mechanism in the C11 standard for read only memory. Check by reading n1570. But be scared of undefined behavior (e.g. writing in some const
data).
In practice, on many C implementations running on current operating systems (e.g. Linux, Windows, Android, MacOSX, ...) and desktops, tablets or servers with an x86-64 or an ARM processor, a process has some virtual address space, with various segments, some being read only (and managed by the operating system kernel with the help of the MMU). Read also about virtual memory & segmentation fault. Take several days to read a book like Operating Systems: Three Easy Pieces (freely downloadable).
On embedded microcontrollers (e.g. Arduino like), some memory might be a hardware ROM. And some compilers might (but are not required to!) use it for some of your constant data.
You might use linker scripts (with GNU ld
) to organize some read only segments into read-only memory. This is very implementation specific.
However, some platforms don't have any kind of user-programmable read-only memory (e.g. some embedded systems have a factory ROM containing a fixed boot loader in firmware, and everything else is in RAM). YMMV.

- 223,805
- 18
- 296
- 547
It's the operating systems which marks pages of virtual memory as either readable, writable or executable (or a combination of all).
The compiler and linker works together to mark special sections of the executable file, and then the operating system loader handles setting up the memory itself.
Nothing of this is part of the C standard, which only specifies that attempting to modify a const
variable is undefined behavior.

- 400,186
- 35
- 402
- 621
-
some RT OS such vxworks supporting no-mmu architecture still use const to mark some memory.if write the const meomory,the system will be suspended – freyone May 14 '18 at 08:33
The complier and linker implement it, for instance in a embeded system, data on RAM is changable, while data on Flash/ROM is not changable.
So if the data is defined with const, it will be place into a non-volative storage place, e.g. Flash/ROM, disk.
Defining a variable with const has two benefits: - Avoid this variable is changed by coding error.(complier error) - Reduce RAM usage, e.g. A long text should be placed into Flash/ROM or disk.
-
1There is absolutely no guarantee or requirement that `const` goes into ROM. This is implementation specific. – Basile Starynkevitch May 14 '18 at 08:32
-
@BasileStarynkevitch It is however industry de facto standard to use `const` for that very purpose on ROM-based system. – Lundin May 14 '18 at 08:39
-
It really depends of your platform and implementation (compiler & linker). There is no guarantee, and you can put read-only data into RAM (e.g. with some linker script). Some systems (even embedded ones) don't have flashable ROM. – Basile Starynkevitch May 14 '18 at 08:41
-
on SingleChip ,on non-OS system on code segment there are some const such as const string,if write these address,what happen? – freyone May 14 '18 at 09:06
-
Depends on the mico-chip, an exception/interrupt shall be raised. – Lin Jianghui May 14 '18 at 09:20