5 pts.
 where does in memory the contant variable in C get store?
Like global,static and local variables get store in data area and stack area respectively,where does the variable qualified as const get placed.Thanks in advance.

Software/Hardware used:
ASKED: July 8, 2008  11:25 AM
UPDATED: July 8, 2008  2:20 PM

Answer Wiki:
Technically, variables of this type are considered Static constants (static const). When executing, these would be created in ROM. As you know, PC's don't have ROM, so the stack is mimicked in RAM. This mimicking is actually the process of grouping the static constant variables with other variables in RAM that are static but are not constant. Essentially, from what I understand, they don't really get their own section because there isn't ROM for them. If you want to be more specific in the placing of your variables, consider using register variables (register int yourvar;) as this memory is allocated within the CPU itself for very quick access.
Last Wiki Answer Submitted:  July 8, 2008  1:38 pm  by  Schmidtw   11,220 pts.
All Answer Wiki Contributors:  Schmidtw   11,220 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Take this with a grain of salt, but I believe they are not stored in either the stack or the heap. Because they’re constant, consts are evaluated at compile-time and plugged straight into the machine code. So as an example, if you have a const AVG_CHILDREN=2.5, then familySize = 2+ AVG_CHILDREN; is exactly the same as familySize = 4 + 2.5; (which would itself be optimized to just familySize = 6.5;).

The advantage of using the const as opposed to just typing in 2.5 is that if the average number of children per family changes, you have to alter just one line of code — the const declaration — instead of finding every instance of that 2.5 and changing it.

 905 pts.