where does in memory the contant variable in C get store?

5 pts.
Tags:
C++
Const statement
Variables
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.

Answer Wiki

Thanks. We'll let you know when a new response is added.

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.

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • YuvalShavit
    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 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following