I was reading Head First C book where i read we can not change a String literal because it is stored in a constant pool of memory which is read only memory. So friends is this constant pool of memory and System's ROM both are same thing. Or within memory other than ROM there is also another constant pool of memory. If i declare a string it will be stored IN ROM while if i declare a int or anything else it will go in RAM?is this true
-
You need to clarify whether you are talking about a generic computer (which may have a hard drive) or an embedded system (where the program must be stored in non-volatile memory). – Feb 27 '14 at 12:15
-
Here you can find a good explanation: http://stackoverflow.com/questions/2482255/c-memory-layout-of-c-program-execution – Fekete Ferenc Feb 27 '14 at 12:23
3 Answers
No, they certainly are not the same thing.
There is no requirement that the string is stored in ROM; if that were the case then how would you be able to load a program from hard disk into RAM and run it? You can't "load into ROM" since the ROM isn't writable.
For a typical modern PC-level computer, it means that string literals are loaded into a part of RAM that is made read-only when the loading is done.
Of course, for embedded systems it can also mean that string literals can validly be placed in actual read-only memory (flash or genuine ROM) since the data cannot validly be modified. This is handy for embedded systems, where the amount of "code space" (flash or ROM) is often much larger than the amount of available RAM.
To summarize, it's a rather generic specification: it just says that from the program's point of view, string literals are to be considered non-modifiable. It doesn't say how that is implemented. It's perfectly possible for the literals to be modifiable, i.e. for there not to be any form of protection against trying. That doesn't mean it's suddenly become valid, just that the implementation isn't protection you against yourself.

- 391,730
- 64
- 469
- 606
-
This memory can only be made read only on modern processor achitectures with advanced memory management implemented in hardware. So for example in old 8 and 16 bit systems this typically wouldn't be possible. – AnthonyLambert Feb 27 '14 at 11:41
No they are not stored in the ROM actually they are mapped into the process space as read-only. Also to mention that it depends and varies from platform to platform.
However if you are looking to modify your string then better use an array as compiler will arrange for the array to get initialized from the literal and you can modify the array.

- 168,305
- 31
- 280
- 331
NO not hardware ROM, a string literal is stored in a read only sector in RAM only (inside Process space) but some simpler chip architectures might not support read only segment in those cases it would be editable.
Note: read about process space (text,data,stack) you will know more about it Check Here

- 1,410
- 13
- 23