0

Where in memory are the const function parameters stored in C?

Generally the parameters are stored on the stack, but if the parameter is declared const then where it is stored?

Jens
  • 69,818
  • 15
  • 125
  • 179
  • http://stackoverflow.com/questions/14588767/where-in-memory-are-my-variables-stored-in-c http://stackoverflow.com/questions/1576489/where-are-constant-variables-stored-in-c http://stackoverflow.com/questions/16304705/where-are-parameter-variables-stored-in-memory – Sachith Wickramaarachchi Apr 02 '17 at 06:51

2 Answers2

1

Same as any other parameter (implementation dependent). The const qualifier only means the function won't modify its parameter. It doesn't mean the parameter is a compile time constant expression that can be eliminated.

Also, compilers aren't bound to passing arguments/parameters on the call stack (if such a thing exists for an implementation). They can be passed in registers as well, if the compiler deems it better.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • How About a static parameter ? – user3865070 Apr 02 '17 at 06:55
  • @user3865070 - Parameters can't be static, so it's a moot question – StoryTeller - Unslander Monica Apr 02 '17 at 06:55
  • The Reason i am asking is why static is not allowed and why const is allowed ? – user3865070 Apr 02 '17 at 07:17
  • @user3865070 - Because `static` is a storage duration qualifier for variables, it controls their lifetime (a parameters cannot exist for the duration of a program, it exists for a single function call). `const` qualifies a variable as non-modifiable by the program and is completely orthogonal to the variables lifetime. – StoryTeller - Unslander Monica Apr 02 '17 at 07:19
  • @StoryTeller: True but to be fair I cannot see any obvious reason why parameters could not be allowed to have a persistent storage duration, semantically equivalent to assigning the parameter into a static copy at function entry and referring to that object in its place. I'm not certain but I believe that Fortran allows this via `SAVE` and it might be marginally useful in rare cases, say something akin to a `register`-style optimizer hint to preserve stack space in recursive functions. Though frankly such a feature would probably mostly serve as a source for bugs. – doynax Apr 02 '17 at 07:43
  • @doynax - Parameters have function scope. Specifying them as static *could* be no different than a static function local variable. But since parameters are meant to constantly be filled with function arguments, letting programmers specify persistence seems pointless. – StoryTeller - Unslander Monica Apr 02 '17 at 07:48
  • @StoryTeller: Sorry, I don't quite follow you. Such a parameters would share a lexical function scope equivalent to static local variables, though with multiple reinitializations on each function invocation. In the presence of recursion the semantics would differ on return where the original parameter value having been replaced by the most-recent value in an invocation, or in threaded programs of course. Not terribly useful and highly prone to reentrancy bugs but I could easily envision an C having included such a construct for orthogonality. – doynax Apr 02 '17 at 07:59
  • @donynax - *"In the presence of recursion the semantics would differ on return"*: so you'd need a call stack to not clobber that location anyway :) The whole point of persistence is for the value to be available across different function invocation. Since the parameter isn't really persistent I think it's a good thing the committee didn't allow it. It'd be very much like `register` parameters, another example of something redundant when all is said and done. I think such things are best left to implementations where the specifier is not a mere recommendation. – StoryTeller - Unslander Monica Apr 02 '17 at 08:01
  • @StoryTeller: I think I am being unclear here. The point is that they would _intentionally_ be clobbered and the programmer would be responsible for determining whether their reuse is safe and intended, much as in the case of `static` variables. Perhaps potentially useful in deep recursive functions to save stack space on forwarded constants and a nasty means of passing back additional state the level, roughly equivalent to the (reentrant) scheme of passing along a context structure. – doynax Apr 02 '17 at 08:09
  • @doynax - I certainly understand what you meant now much better. And I think *that* scheme would be more trouble than it's worth. – StoryTeller - Unslander Monica Apr 02 '17 at 08:12
  • @StoryTeller: Certainly, I also fully endorse their exclusion from the language. I was putting myself in the shoes of @user3865070 and thinking that the lack of support for of `static` parameters may be less than obvious or indeed necessary. – doynax Apr 02 '17 at 08:15
0

const keyword means that data with this keyword won't be able to modify content.

For example, if function has parameter with const:

void my_func(const char c) {
    //Do things
    //This is prohibited and will cause error:
    c = 3;
}

//Function call
char a = 'a';   //This is stored on RAM somewhere (if inside function, then it is on stack)
my_func(a); //Call function, value of a is pushed to stack.

This function cannot change value of c inside.

If you have global variable with const parameter, then some compiler will put it to FLASH memory (non-volatile) memory. This happens on embedded systems mostly, but it is not sure that will actually happen.

//This is stored either on RAM or on non-volatile memory (FLASH)
const char str[] = "My string which cannot be modified later";
int main(void) {
   //...your code
   //This is prohibited:
   str[2] = 3; //Error!
}

This str is constant and is not able to be modified. Now, modern compilers (ARM compilers) will put this to flash memory, on AVR, this might now happen. There you have to add PROGMEM keyword of GCC for AVR.

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40