On 5 April 2014 17:45, smplx wrote: > > On Sat, 5 Apr 2014, John J. McDonough wrote: > > > In "proper" C, local variables are allocated on the > > stack, which takes essentially no overhead (the compiler increments the > > stack pointer by n+1 instead of 1). > > This is not true. Most processors have an overhead ***EVERYTIME*** they > access a stack relative variable. This might be something as trivial as a= n > extra clock cycle for every machine instruction that uses stack relative > addresses to something much more serious such as having to calculate the > address of each such variable at runtime (which can take several machine > instructions). > I guess that highly depends on the architecture. Many modern CPU uses pipelining and the memory arithmetic is calculated within that -- meaning virtually no clock penalty is used for accessing data through frame pointers. However, preparing the frame may take some time even if not that much. Some MCU compilers though avoids using traditional stack frames and turns local variables to overlay ones instead. > Sometimes it is better to use globals than locals because of the speed > advantage. > In that case in my opinion it is better to use static or overlay instead so you can manage the visibility of your variables better, plus in case you use overlay, you may can save up some space. I guess globals are "great" when you need to pass data from one function to the other, so actually you do not even need to pass them -- kinda "not nice" and against some programming principals, but highly efficient :-) Of course you cannot use globals, statics or overlays for functions that needs to be reentrant. Tamas > > Regards > Sergio Masci > -- > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > --=20 int main() { char *a,*s,*q; printf(s=3D"int main() { char *a,*s,*q; printf(s=3D%s%s%s, q=3D%s%s%s%s,s,q,q,a=3D%s%s%s%s,q,q,q,a,a,q); }", q=3D"\"",s,q,q,a=3D"\\",q,q,q,a,a,q); } --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .