Pages: [1]   Go Down
Author Topic: Increased code size with global/local RAM accesses  (Read 487 times)
0 Members and 1 Guest are viewing this topic.
Piemonte, Italia
Offline Offline
Jr. Member
**
Karma: 1
Posts: 65
Just a brick in the wall
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The title is not too proper, sorry. Please read on: I had the following piece of code:

Code:
Relay relay = relays[i];

relay.delay = atoi (param);

This wasn't working properly, since of course I'm modifying a new temporary Relay instance, while I should work on the instance contained in the array. To make a long story short, what I wanted to write was:

Code:
Relay& relay = relays[i];

relay.delay = atoi (param);

Basically I just wanted to avoid having to type
Code:
relays[i]
every time, so I defined a reference to save some typing smiley. I know it's just a few chars in this case, but this is something I often do, as I think it also makes the code clearer. Anyway, before making the fix, the binary size was 28294, after that it was 28516: an increase of more than 200 bytes! Is this something expected?

It doesn't make much sense to me. I mean: this is not directly due to the reference, since if I just remove it and replace
Code:
relay
with
Code:
relays[i]
everywhere the code size stays the same. So the actual question is: is the code to access a global array so much bigger than the one that accesses the stack?
« Last Edit: August 24, 2012, 01:09:01 pm by SukkoPera » Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I would suggest verbose compiling both versions and comparing the two different assembly files that are produced to see what the differences are.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 93
Posts: 4749
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Relay relay[ some_number ];
Relay *relays = relay;  // initially points to relay[0] but can relays++ or relays += # or relays--, etc.

Globals use up ram from the start, locals go on and off the stack which is also ram.
You can get in trouble with either but it's easier to count global bytes and know they're there.
Also, globals _can_ be allocated and initialized once and addressed directly.
 
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I know it's just a few chars in this case, but this is something I often do, as I think it also makes the code clearer.
Sorry to disagree with you. But, using reference variables to access an array element is counter-intuitive to me. It is much more obvious that the relay being operated on is an array element when the array notation is used.
Logged

Piemonte, Italia
Offline Offline
Jr. Member
**
Karma: 1
Posts: 65
Just a brick in the wall
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for all your comments.

@majenjo: Good idea but unfortunately I'm not proficient in Atmega assembly.

@GoForSmoke: thanks but your comment is totally off question smiley.

@ PaulS: I guess it's a matter of taste/style, at my previous job I had to deal with lots of code like the following:

Code:
somearray[someindex].track.geodata.latitude.value = ...

Repeated for tenths of attributes. This is just plain silly and unreadable, IMHO. Sometimes the lines were so long that the right side of the assignment was out of the screen and you had to scroll to understand what was going on. Plus, if someone renamed, for instance, "geodata" to "position", you had to change it all over the place. Using the reference avoids all of this and the fact that you're using an array is actually explicited in the reference definition.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 93
Posts: 4749
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The position of

Code:
somearray[someindex].track.geodata.latitude.value

is a pointer to somearray[ i ] plus some constant offset.

Possibly the smart thing is to make a class that inherits your Relay class and adds simple-named member functions to access the data members in question.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: