Question:
when we take a variable as const type in C where it could be stored?
Manish Thakur ji
2010-04-14 03:43:18 UTC
this question is about the C programming language where the memory reserved for a const type variable? is in the CPU or in primary memory or ROM
Four answers:
Jonas
2010-04-14 03:51:58 UTC
As it is up to the compiler to implement the const qualifier, it isn't possible to say if it would affect where in memory it would be stored (stack or heap).



If you declare it in a function, there would most likely be reserved memory on the stack, and as a global variable it would be placed in the heap, but again, it is pretty much up to the compiler.
Joseph
2010-04-14 14:57:15 UTC
It's up to the compiler, and depends on type and scope, and possibly other things. That's the best answer you can get.



If it's a basic type, like an integer or something, the compiler may decide not to reserve space for it at all, and instead simply cut and paste the literal value everywhere you use the variable.



If the const variable it's used as a parameter to a function, it will probably be in a register or on the stack.



If it's stored in memory and not a function parameter, it may be in the data section of "ROM", and whether or not it's declared global or static may change the location as well.



To actually see where it is on a given compilation, simply print out the address of your const variable (or use a debugger), and compare it with the addresses of other non-const variables.
Ismail
2010-04-14 03:55:02 UTC
It is stored on Primary Memory (RAM).When ever cpu needs it , it will be loaded in the cpu register for procession.
2016-06-01 08:31:43 UTC
As well as the previous answer, remember that you have the 'Process' which you can use to run external commands and applications. Below is an example


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...