Go Down

Topic: int and #define question (Read 103 times) previous topic - next topic

My understanding is that int sets a variable and #define makes a constant.
My question is by using does using int to set a constant instead of #define use more RAM?

int SOLENOID = [0, 360, 129600, 46656000];

By using #define in the program instead of int and the number string is longer than the symbol. When the sketch is compiled does it use more of the program space?

eg

#define SOLENOID 0, 360, 129600, 46656000

If I need all the programming space I can get which of the two am I better off using to define a constant?

goodinventor

No. Both use the same amount of RAM. The only difference is how the processor executes it.

PaulS

Quote
No. Both use the same amount of RAM.
Wrong. A # define statement defines a name and value. The preprocessor substitutes the value wherever the name appears in the code.

Whether that results in using SRAM, or not, depends on how the name is used in the code, and what the value is. There are NO concrete answers.

PaulS

Code: [Select]
int SOLENOID = [0, 360, 129600, 46656000];
How about posting an example that has slightly more than a snowball's chance in hell of compiling?

KenF

The #define just tells the compiler how to interpret one expression as something else before it compiles it

No variables are used in the final sketch.

For instance.  As a demonstration of terrible coding you could do something like this.

Code: [Select]

#define eggs void setup(){
#define bacon void loop(){}
#define mashedPotato Serial.begin(
#define saxaphone 9600);
  
//So now we write the actual sketch

eggs
mashedPotato saxaphone
Serial.println("Hello World");
}

bacon

michinyon

Your question is so riddled with false assumptions,  that it is difficult to know how to address it.

If it is "programming space" that you are short of,   then you need to consider minimising the amount of machine code ( and constants embedded in it ),  that your program gets compiled to.   The program variables are in RAM  and don't consume the same flash memory that the program occupies.   However,   the initialisation of them does.

The length of the names of your variable names, function names,  or any other user-defined keywords makes no difference,  because they are generally not present in the final linked and compiled code.   Likewise,  the length of your numerical constants, as expressed in source code/text form,  doesn't matter.

If you want to find out how various code formulations affect the executable program size,  there is nothing stopping you from doing your own experiments.   You will need to use the correct language for the environment you are compiling for, however.   Is it Matlab code,   that you have written there ?  Matlab is usually an interpreted, not compiled, language.

jurs

My question is by using does using int to set a constant instead of #define use more RAM?

int SOLENOID = [0, 360, 129600, 46656000];

By using #define in the program instead of int and the number string is longer than the symbol. When the sketch is compiled does it use more of the program space?
Your program uses as much RAM as you declare. If you declare an integer-array with 4 elements, the RAM used is 4*sizeof(int)= 4*2 = 8 bytes.

The problem with your declaration is: 129600 and 46656000 are not in the range of 'int' variables. So if you perhaps want to have different information all together, perhaps you want to declare a user defined data structure with the "struct" statement? In a struct each element can be declared with a different type.

Example code using a 'struct' data structure:
Code: [Select]

struct solenoid_t {byte pin; int minValue; int maxValue; long ticksLeft; long ticksRight;};
solenoid_t solenoid= {2, 100, 360,  129600, 46656000};

void setup() {
  Serial.begin(9600);
  Serial.println(solenoid.pin);
  Serial.println(solenoid.minValue);
  Serial.println(solenoid.maxValue);
  Serial.println(solenoid.ticksLeft);
  Serial.println(solenoid.ticksRight);
}

void loop() {}


The 'struct' in that case has one element of size 'byte', two elements of size 'int' and two elements of type 'long', as you like to declare and use the data. And each element has got its own 'name' for the sub-element, so you can easily write 'speaking code'.

Nick Gammon

My understanding is that int sets a variable and #define makes a constant.
What about "const int"?

Code: [Select]
const int foo = 42;
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

#8
Jan 31, 2015, 02:19 pm Last Edit: Jan 31, 2015, 02:31 pm by happyslappyno1
Thanks for your answer Nick. I have programming experience for 36hrs and yours seems like an answer I understand.

@michinyon I find your answer very Logical.

I'm planning an approach to programming a parabolic mirror to follow the sun using arduino, shield and stepper motors. Also plan on automating aquaponics and using the ATmega? I dont know yet if I can expand pins on an atmega or have to use one atmega to trigger a setup in another atmega if I run out of space or pins.



Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy