LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Most efficient way to use constants in C. (http://www.linuxquestions.org/questions/programming-9/most-efficient-way-to-use-constants-in-c-841235/)

darkstarbyte 10-29-2010 02:29 PM

Most efficient way to use constants in C.
 
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?

dugan 10-29-2010 03:31 PM

Well, this is how you use them:
Code:

#DEFINE MAX 1
...
count = MAX;

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.

darkstarbyte 10-29-2010 05:15 PM

My programming book said they used it to save ram in the 80's then he said its not a big deal anymore but I thought I would ask.

devnull10 10-29-2010 06:49 PM

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.

dugan 10-29-2010 08:50 PM

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 (Post 4143806)
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.

Sergei Steshenko 10-30-2010 12:41 AM

Without using named constant it's impossible to effectively maintain code, especially, code written by somebody else.

Look up:

DRY (Don't Repeat Yourself) principle
single point of change
.

theNbomr 10-31-2010 01:08 PM

Quote:

Originally Posted by devnull10 (Post 4143806)
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.

Plus, what Sergei said...

--- rod.


All times are GMT -5. The time now is 01:08 PM.