views:

128

answers:

4

hi,

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 from read only memory?

is it OS dependant?

A: 

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.

sharptooth
+3  A: 

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.

R Samuel Klatchko
A: 

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).

ShinTakezou
A: 

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).

Pete Kirkham