Pages: [1] 2 3 4   Go Down
Author Topic: Reforming datatype practices  (Read 3004 times)
0 Members and 1 Guest are viewing this topic.
Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?  smiley if so, that is great.

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

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#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.
« Last Edit: November 19, 2008, 01:16:50 am by mem » Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now I feel bad for sounding harsh  smiley-wink

Jason, thanks for clearing that up  smiley
« Last Edit: November 19, 2008, 09:52:06 am by mem » Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley

-j

Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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]
« Last Edit: November 19, 2008, 12:09:20 pm by AlphaBeta » Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 511
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#define is just a macro
Code:
#define pin 13
...
digitalWrite(pin,HIGH);

is identical to:
Code:
digitalWrite(13,HIGH);

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

Code:
#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:
#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.
« Last Edit: November 19, 2008, 12:43:25 pm by dcb » Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: November 19, 2008, 12:45:17 pm by mem » Logged

Portland, OR, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 78
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
static const uint8_t pinLED = 13;
Logged

Don

ZBasic Microcontrollers
http://www.zbasic.net

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You can also define a variable with the const attribute to serve the same purpose.
Hi  don, indeed you can , but
Code:
#define pinLed 13
seems easier for beginners  than
Code:
static const uint8_t pinLED = 13;
« Last Edit: November 19, 2008, 01:27:47 pm by mem » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: November 19, 2008, 02:02:04 pm by AlphaBeta » Logged

Pages: [1] 2 3 4   Go Up
Jump to: