Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #45 on: September 30, 2012, 03:56:22 pm » |
A fairly simple fix in fact is to have a naming convention for class variables, eg. int m_myVariable; // m_ = member; or: int myVariable_; // using trailing underscore on class variables
|
|
|
|
|
Logged
|
|
|
|
|
North Queensland, Australia
Offline
Edison Member
Karma: 31
Posts: 1179
|
 |
« Reply #46 on: September 30, 2012, 06:27:16 pm » |
Yes I prefer namespaces or trailing '_G' The example is a good point I think as many are avoiding 'this->' to keep their code 'pretty', whereas blatant use will save you without even knowing it was a problem. i_NewVal is fine as its callee scope is non-dependant. Further templated functions would require further qualification. The important part of my post was showing how the global was the sole cause of problems, the code otherwise would not have compiled. So rather than receiving an error about 'i' being undefined in the class ( 'this->' is required to make the name dependant ), the previous definition of 'i' is silently used.
|
|
|
|
|
Logged
|
|
|
|
|
NZ
Offline
Jr. Member
Karma: 1
Posts: 74
|
 |
« Reply #47 on: November 22, 2012, 03:43:37 am » |
Interesting and thank for clarifying this.
One question tho
When allocation static memory does that automatically become a global variable ?
Kim
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #48 on: November 22, 2012, 03:56:57 am » |
I'm not sure I understand your last question... Are you asking if qualifying a variable as "static" makes it global ?
Scope and lifetime are two related but distinct properties of a variable.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Edison Member
Karma: 42
Posts: 2194
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #49 on: November 22, 2012, 05:24:13 am » |
Interesting and thank for clarifying this.
One question tho
When allocation static memory does that automatically become a global variable ?
Kim
No. A global (i.e., defined in the "global" scope) variable and static variable are different. A global can be accessed anywhere. A static can only be accessed in the function it is defined in. A static variable retains its value across subsequent calls to the function. The memory for a static is, as the name suggests, static - it is always at the same location, so theoretically it can be accessed globally through the address: int *superGlobal;
void myFunc() { static int localStatic = 4; superGlobal = &localStatic; // Assign address of localStatic to superGlobal pointer localStatic++; }
void myOtherFunc() { int myLocal; myFunc(); myLocal = *superGlobal; // "myLocal" now contains whatever localStatic contains. }
Not something that is done often - it's more common to either return the value from the function, or return a pointer to the static variable. Such functions, though, are not re-entrant, and are frowned on in today's multithreaded environments. Perfectly fine on the Arduino though 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #50 on: November 22, 2012, 06:17:45 am » |
 I hope you are aware that the code above is fine as an "extreme" didactic example, but it's definitely a coding horror(1). And the reason is, it's the perfect recipe for spaghetti code  (1) As in "Code Complete 2"; see Jeff Atwood's blog icon for an effective visual representation of the concept 
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2909
I only know some basic electricity....
|
 |
« Reply #51 on: November 22, 2012, 10:32:00 am » |
Re-entrant code can build up the stack really fast if you're not very, very careful. Remember, UNO has only 2k of RAM for stack and heap space.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
NZ
Offline
Jr. Member
Karma: 1
Posts: 74
|
 |
« Reply #52 on: November 22, 2012, 01:49:44 pm » |
Thanks
Coming from Assembly and a bit of basic, I'm trying to get my head around the GNU C compiler and the way it does things. Been reading the GNU documentation, but sometimes it is "clear as mud" :-)
K
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6344
-
|
 |
« Reply #53 on: November 22, 2012, 02:34:21 pm » |
Re-entrant code can build up the stack really fast if you're not very, very careful.
You're thinking of recursive code?
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2909
I only know some basic electricity....
|
 |
« Reply #54 on: November 22, 2012, 08:11:37 pm » |
Right. Functions that call themselves or call others that call them, etc.
It's been a while since I wrote that kind of stuff.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
UK
Offline
Edison Member
Karma: 42
Posts: 2194
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #55 on: November 23, 2012, 05:11:24 am » |
Right. Functions that call themselves or call others that call them, etc.
It's been a while since I wrote that kind of stuff.
Re-entrant is entirely different. Re-entrant code can be safely called by two different threads at the same time without trashing any global / static variables. They often involve passing of pointers to work areas to the function, so each thread has its own bit of memory.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2909
I only know some basic electricity....
|
 |
« Reply #56 on: November 23, 2012, 07:11:47 am » |
Something else I wouldn't try with a 328P chip.
Long live the state machine!
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
UK
Offline
Edison Member
Karma: 42
Posts: 2194
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #57 on: November 23, 2012, 07:30:24 am » |
Something else I wouldn't try with a 328P chip.
Long live the state machine!
Hell yeah  I had fun a bit back trying to move a few non-re-entrant library routines (from the printf family) into kernel space on RetroBSD so as to reduce user-process size (kind of a pre-cursor to shared libraries) a bit back... Not nice trying to convert them to re-entrant 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 6
Posts: 398
|
 |
« Reply #58 on: November 23, 2012, 12:47:41 pm » |
The String class is kind of like training wheels on a bicycle. Babies need them in order to learn how to ride (or code), but as soon as you want to do anything remotely useful you have to take them off and throw them away.
So many of the "Why don't they fix this bug?" complaints could easily be rewritten as:
"Wah! I want to be able to do BMX tricks but I'm scared to let go of my training wheels."
|
|
|
|
|
Logged
|
|
|
|
|
0
Online
Tesla Member
Karma: 50
Posts: 6540
Arduino rocks
|
 |
« Reply #59 on: November 23, 2012, 02:54:28 pm » |
The String class is kind of like training wheels on a bicycle. Babies need them in order to learn how to ride (or code), but as soon as you want to do anything remotely useful you have to take them off and throw them away.
So many of the "Why don't they fix this bug?" complaints could easily be rewritten as:
"Wah! I want to be able to do BMX tricks but I'm scared to let go of my training wheels."
Wow, another String class whiner!  Sad that the String class whiners almost never correct/adjust the usually simple they constantly whine about. Sad
|
|
|
|
|
Logged
|
|
|
|
|
|