where are the constants?

This is a discussion on where are the constants? within the C++ Programming forums, part of the General Programming Boards category; basically this is a question about memory layout. well there are basically those "blocks": [code] [heap] [stack] [aliased memory] what ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    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?
    signature under construction

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Google + keywords = fun.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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).

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with variables and constants.
    By dave1339 in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 08:18 PM
  2. Replies: 7
    Last Post: 11-12-2008, 10:00 AM
  3. COnstants
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 09-01-2007, 04:59 AM
  4. Constants
    By shuo in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2007, 11:51 PM
  5. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 07:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21