-
where are the constants?
basically this is a question about memory layout.
well there are basically those "blocks":
[code]
[heap]
[stack]
[aliased memory]
what about constants - where are they? like const char *p = "blah";
where is the blah stored?
and afaik that blah exists in an read only memory location - and afaik the code segment is read only too. so are constants together with the code?
or are they in some extra location?
-
Google + keywords = fun.
Quzah.
-
All of these "blocks" will generally be in RAM. It's up to the compiler & operating system to determine the physical location/address. The operating system may move the data between physical RAM, cache, and vitrual RAM (hard drive).
With a high level language and an operating system, the stack and heap are abstract concepts. If you're writing assembly language for an embedded system with no operating system. They will have known physical locations.
Windows uses virtual memory, so when your C++ program uses pointers & addresses, you are not seeing the real physical RAM address.
A constant can be declared on the stack, or on the heap, just like any other "variable." The compiler flags it as a constant, and generates a error if you write code that can change it. It's not really "locked" constant. There are probably ways to change a constant at run-time... maybe using a pointer or somethin'. (?)
If you hard-code a value: X = Y + 2; The "constant" value 2 will be in code-space as part of the compiled machine code.
Constants are NOT stored in Read Only Memory. Your code and your variables cannot be written to ROM! The main thing in ROM is the BIOS. Peripheral & I/O hardware also has ROM. (Your video card/chipset has ROM)
Also, check out FOLDEC (Free On-Line Dictionary Of Computing).
-
Actually, constants might very well be stored in write-protected sections of the virtual address space. Write-protected meaning that the OS will raise an error if anyone tries to write.