Go Down

Topic: Why use int instead of byte for pin numbers? (Read 27671 times) previous topic - next topic

GrooveFlotilla

Quote
They were using signed integers to store that. It would have taken a semi competent programmer of any seniority, let alone architect, to realize that it takes the same space and processing power to just use an unsigned integer,
Java mentality.
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

INTP

When's the last time you ever saw a perfect product that pleased everybody?

MarkT

yea, I am getting close to the edge of ram in one of my project, changed all my const X's to #defines and freed up almost 2 dozen bytes of ram

Surprising that, since const variables are stored in progmem, not in RAM...

Once it has gone through the compiler there is no difference between
Code: [Select]

const byte X = 4;

and
Code: [Select]

#define X 4

Nope, they are different, as has been proven by experiment with the actual compiler and
the actual compiler optimization settings used in the environment.  There is definitely no
requirement to store const variables in read only memory, its just a possible optimization.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Grayed

Pin numbers: How stupid is that? What's wrong with "PORTB, BIT 7"?
Well, to somebody new  to Arduino, here's what's wrong:  I can understand pin numbers.  I don't understand "PORTB, BIT 7"!

Jimmus

Nope, they are different, as has been proven by experiment with the actual compiler and
the actual compiler optimization settings used in the environment.  There is definitely no
requirement to store const variables in read only memory, its just a possible optimization.
I have found this to be true.  I would have thought that the compiler would treat a constant variable like a constant instead of a variable.  I like having the type, and have historically used const byte ledPin = 3; sort of statements for easy reading.  However, I found (incidentally, when I was debugging something unrelated and noticed it in the generated assembly code) that in some instances it generated larger code than #define statements.  So I switched.  I prefer to have more efficient execution code than prettier source code.  I realize this is a value judgement, and there are many people who feel that maintainability and speed of coding are more important than execution speed.  I prefer to have a better end product.

Hutkikz

I believe that this is guaranteed to be a compile time constant.
Code: [Select]
static constexpr byte x = 4;

Coding Badly

However, I found (incidentally, when I was debugging something unrelated and noticed it in the generated assembly code) that in some instances it generated larger code than #define statements.
Example please.


Go Up