开发者

read-only memory and heap memory

AFAIK, string literals are stored in read only memory in case of C language. where is this actually present on the hardware.

as per my knowledge heap is on RAM.correct me if i am wrong.

how different is heap fr开发者_StackOverflowom read only memory?

is it OS dependant?


It is usually done with hardware assistance.

The virtual memory subsystem of the hardware can be told to mark a page as read-only. When an application tries to write a read-only page, the hardware generates a fault that the OS catches. The OS can tell that the app tried to write a read-only page and end your program.

So the OS/loader makes sure the pages the string literals are in are marked as read-only.

The heap and read-only memory are orthogonal issues.


It's OS and hardware dependent. The spec says they can be placed in read-only memory, rather than they have to be. If you're writing C for a simple embedded device, then the strings get blown into the rom and runtime memory is allocated from the RAM; these are physically separate (Harvard). If it's a typical unix-like computer, then there is virtual memory subsystem which converts logical addresses to physical addresses in pages and can mark some pages read-only and some executable, but the memory itself can be either data or instructions (Von Neumann).


Usually that's ordinary process virtual memory with write protection set.

That's implementation-dependent, but processors usually use special metadata blocks for controlling access to memory regions and the operating system can set those accordingly. So the string literals and all other unchangeable stuff is loaded into a region that is set protection on. So when the program tries to modify that memory a special unit inside the processor checks whther that write is allowed and if it is not wllowed issues a hardware interrupt that is handled by the operating system.


From an hw point of view RAM is RAM and it can be R/W. The "read-only" memory is an attribute put by software; on some architecture (almost all we're used to) there is hardware support to make a portion of memory not writable (in the sense that when you try to access an address inside that memory, an "exception" occurs).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜