- Home /
how do variables vs constants work in compiled code?
So I imagine in the compiled code, wherever you use a variable it saves a pointer to the variable in the ram, right? With constants does it still use a pointer, or does it just put the value instead?
I want to know because I want my codes values to be editable easily by people in the editor, but I also don't want to effect the ram by to many variable pointers, which if constants are just values instead of a pointer then I could use those with out effecting the ram any more, right?
Answer by elenzil · Jul 19, 2016 at 12:23 AM
i believe that whether something is a const or not is only a compile-time feature. ie, it just enforces what source-code is allowed to do, not what RAM looks like.
however, a 'const' is automatically a static, which means you only have one of it shared by all instances of a class, instead of one for each instance. so potentially there's some RAM savings there.
however, my advice would be strongly to not worry about the RAM usage here. unless you're talking about something which has many 10s of 1000s of instances, it just doesn't matter. you will be much better off if you design your code to be easy to maintain.
if you know that a value will never change, you should make it const, because that lets other people (like your future self) know that it will never change, and thus makes the code easier to read.
Answer by wojtask12 · Jul 19, 2016 at 12:08 AM
not all of the variables are represented by reference. For example, int,string or any struct are scalar while all classes are reference types. plus, you said you want these values to be editable by people in the editor. Constant can't be edited - that's why it's called constant