Go Down

Topic: Reforming datatype practices (Read 5015 times) previous topic - next topic

AlphaBeta

I have always wondered why all examples in the arduino enviroment use int where not needed. Why not use byte?

byte bLedPin = 13;

This uses half the memory and is, in my honest opinion a much better practice especially in a microcontroller enviroment.

[the prefix b might be omitted, ofcource, but it is quite standard to prefix variables so why not encourage it?]


This is more a question than anything else, I just have to know if there is any good arguments for using ints where not needed.

kg4wsv

I suspect it's a matter of habit, left over from C programming on systems that aren't memory constrained.

I'll take it a step further - I don't see why these are variables at all.  To use your LED example, are you going to change which pin the LED is connected to while the program runs?  I think it should be #defined.

-j


AlphaBeta

Indeed it was a stupid example. But I though about all loops and such.

Also, who not use the keyword unsigned a bit often? It can be helpful that the compiler let you know that it may be a stupid thing to do to try and set a led dim to -2, for instance.

I have always wondered how the #define stuff works, does the number get 'hard coded' into hex in the resulting hex file? [i thought it was just a simple find/replace operation] or how does it work? You see, I though that it would take 2bytes if defined, that arduino defaults to ints but that is wrong?  :) if so, that is great.

I vote for 'better' practices concidering datatype and use of unsigned datatypes.

mem

#3
Nov 19, 2008, 07:16 am Last Edit: Nov 19, 2008, 07:16 am by mem Reason: 1
#defines are more efficient because the compiler can hard code the values in the hex file. But sometimes a variable is necessary, for example when using an array of pins, and using byte is more efficient than int where a variable is necessary.

I think the arduino team is rightly more concerned about how easy an example is for the user to understand than how efficient the code will be. But the examples do influence user style and I also vote for introducing the use of the right sized data types (and #defines) in example code.

kg4wsv

A #define (and all other preprocessor directives, e.g. #include , #ifdef, etc) are handled by the C preprocessor (cpp) and not the compiler.  #define is a basic search and replace text operation that takes place before the compiler runs.

-j


mem

Quote
A #define (and all other preprocessor directives, e.g. #include , #ifdef, etc) are handled by the C preprocessor (cpp) and not the compiler.  #define is a basic search and replace text operation that takes place before the compiler runs.

That is correct but misses the point being made above. The compiler is more likely to optimize values that are #defined than values that are declared as normal variables.  

kg4wsv

Sorry, I should have quoted for clarification.  I wasn't commenting on (or correcting or arguing with) anything you said, mem, I was trying to answer the question "I have always wondered how the #define stuff works".

-j


mem

#7
Nov 19, 2008, 03:51 pm Last Edit: Nov 19, 2008, 03:52 pm by mem Reason: 1
Now I feel bad for sounding harsh  ;)

Jason, thanks for clearing that up  :)

kg4wsv

no problem, didn't sound harsh to me.  tone is difficult to pick up, that's why on the internet we a) don't take it personally, and b) use those smiley things.

on all the points you brought up, we are in violent agreement.  :)

-j


AlphaBeta

#9
Nov 19, 2008, 06:01 pm Last Edit: Nov 19, 2008, 06:09 pm by AlphaBeta Reason: 1
Quote
I think the arduino team is rightly more concerned about how easy an example is for the user to understand than how efficient the code will be.


Ofcourse, but I would argue that it equally easy to comprehend the byte datatype, as opposed to ints. In microcontroller world you are bound to either come across bytes and bits, or if you are an artist using arduino for leds and such, you already know that you need some mega bytes to store tha program. I do think that it is rather intuitive that the byte is a variable that can hold data.

Two last questions about #define:
1)  are the variables then treated as constants by compiler?
2) does not arduino store the defines/constants in a integer datatype? [or does compiler check for how many bytes needed, tried google but no luck]

dcb

#10
Nov 19, 2008, 06:39 pm Last Edit: Nov 19, 2008, 06:43 pm by dcb Reason: 1
#define is just a macro
Code: [Select]

#define pin 13
...
digitalWrite(pin,HIGH);


is identical to:
Code: [Select]

digitalWrite(13,HIGH);


You can put just about anything in a #define, i.e.

Code: [Select]

#define pinOn digitalWrite(13,HIGH)
...
pinOn;


You can also paramaterize the macro, Don K posted a handy example here for determining the length of an arbitrarily typed array:
Code: [Select]

#define ITEM_CNT(x)  (sizeof(x)/sizeof(x[0]))


While conserving every last bit and byte is important (when you start running out of bits and bytes), being able to do things generically and reducing maintenance are good things too.

mem

#11
Nov 19, 2008, 06:44 pm Last Edit: Nov 19, 2008, 06:45 pm by mem Reason: 1
Two last questions about #define:
>1)  are the variables then treated as constants by compiler?


#defines of integral values are not variables, they are treated as constants

> 2) does not arduino store the defines/constants in a integer datatype? [or does compiler check for how many bytes needed, tried google but no luck]

The compiler uses the smallest data type that will hold the constant.

Don Kinzer

Quote
#defines are more efficient because the compiler can hard code the values in the hex file.
You can also define a variable with the const attribute to serve the same purpose.  The compiler will substitute the value as an optimization instead of allocating space in RAM for it (assuming there are no references taken of it).  The added benefit is that the compile can type check uses of the "constant".
Code: [Select]
static const uint8_t pinLED = 13;
Don

ZBasic Microcontrollers
http://www.zbasic.net

mem

#13
Nov 19, 2008, 07:26 pm Last Edit: Nov 19, 2008, 07:27 pm by mem Reason: 1
Quote
You can also define a variable with the const attribute to serve the same purpose.

Hi  don, indeed you can , but
Code: [Select]
#define pinLed 13
seems easier for beginners  than
Code: [Select]
static const uint8_t pinLED = 13;

AlphaBeta

#14
Nov 19, 2008, 08:01 pm Last Edit: Nov 19, 2008, 08:02 pm by AlphaBeta Reason: 1
My reason for discussing the #define is the fact that the compiler can help with constants, just as don said. [I forgot to explicitly say this, but I somewhat tapped into it while argumenting for unsigned usage]

I do agree that #define seems easyer for beginners, and because of this a better arduino practice on pin declaration.

Go Up