ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What is the best way to use constants in C? I have heard that constants can save ram but my question is do you use it on immediate values, variables that don't change, or codes that repeat them selves a lot?
What is the best way to use constants to save ram?
The C code is then compiled in two steps. First, the preprocessor does a search-and-replace and replaces every instance of "MAX" with "1". Then the code is compiled as normal.
As you've guessed by now, you use constants to represent named constants. Another example would be:
Code:
#define PI 3.14
Their usual use is to make code more readable, by giving human-readable labels to machine-readable values.
Now please tell us where you learned about using constants to save RAM. That's not what they're for. The amount they save is absolutely negligible unless you're deploying to an embedded controller, and probably is even then.
It's not a big deal because everyone has so much RAM and because compilers can do a hell of a lot of optimization these days! It's always worth sticking to principles though.
Do keep in mind that the compiler needs to allocate a temporary variable for the value of the constant anyway.
Quote:
Originally Posted by devnull10
It's always worth sticking to principles though.
If you were worried about this then you wouldn't be using functions either. Pushing function addresses and their parameters onto the stack takes memory too.
Of course, in the 80s the speed difference between compiled C code and assembler was still tangible, so that might actually have been the case.
Today's principle is to code for maintainability, and wait until you know what needs to be optimized before you start optimizing.
It's not a big deal because everyone has so much RAM
Only slightly off-topic:
This is a general programming forum, so it isn't necessarily the case that RAM is virtually unlimited (pun intended), for all of us. There are still small processors and micro-controllers that have limited memory, and that also have distinct types of memory, such as ROM/EPROM and RAM. In that context, the term 'constant' takes on additional meaning, because a value stored in ROM cannot be written (in fact, the CPU instruction set may have no instruction to do so). If the CPU boots from ROM, then some 'constants', like literal strings, may need to be copied from ROM to RAM by the startup code. Keeping track of what is writable, and what is not then becomes important.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.