Prev: Using pointer-to-member of incomplete type in constructor call in VS2008
Next: filename string manipulation
From: Tim Roberts on 7 Feb 2010 14:13 Robby <Robby(a)discussions.microsoft.com> wrote: > >But just to reminisse here, so then lets do everything with functions. Why >bother with macros at all. Why even keep macros in the C language standard. Yes, indeed. MANY people have asked that exact question, including Bjarne Stroustroup, the guy who created C++. He wanted to get rid of the preprocessor altogether. Templates and "const" variables are both attempts to eliminate misused preprocessor features. >Also, I *think* having an important calculation such as the system frequency >calculation, would be a calculation deemed important to be set up there for >it to be globally defined as a macro. I might need this calculation on the >fly without going through a bunch of functions to find the one function that >does this very specific calculation. Anyway its just my *personal* opinion. I don't see the difference between "going through a bunch of functions to find the one" and "going through a bunch of macros to find the one". The search process is the same, and the function choice is safer. -- Tim Roberts, timr(a)probo.com Providenza & Boekelheide, Inc.
From: Robby on 8 Feb 2010 17:58 "Tamas Demjen" wrote: > Robby wrote: > > > So, in C, we *can't* have global static variables. I guess it would make > > sence though. If a variable is global, why would it need to be static. Its > > global! Its contents will never be lost!!! > > I think you're mixing the terminology. The static keyword is context > sensitive, and depending on where it appears, it has a completely > different meaning. Declaring a variable in the global scope has nothing > to do with it being static or not. > > Variables have a lifetime (duration) and a visibility, and those > concepts are more or less independent. > > The global scope means you're not inside a function, structure, or > union. In simple terms, you're not inside a curly brace. Variables > in the global scope have the same lifetime as the application. > They never go out of scope, so they never lose their values. > Variables inside a function normally lose their values when the > function exits, unless they're declared static. > > Visibility can be different than lifetime. A variable can be visible > inside a scope, in an entire .c file, or inside the entire executable. > In case of a DLL/shared object/dylib, you can also control whether a > variable or function is exported (available to the client of the > library). > > Let's see an example for global and local scope: > > // main.c > #include <stdio.h> > > int global_scope; > // lifetime: global (until the app exits) > // visibility: global (for the entire executable/module) > > void f() > { > int local_scope; > // lifetime: local (till the function exits) > // visibility: local (for this function only) > } > > If you use the static keyword in the global scope, it means the variable > or function is only accessible in the translation unit where it's > declared: > > static int internal_global; > // lifetime: global (until the app exits) > // visibility: local (for this .c unit only) > > It's in the global scope, so it has a global life span, but it's local > in terms of accessibility. You can't refer to it from the outside. > static means it can't be extern, and other .c translation units can't > see it. You can define another internal_global variable in another .c > file without conflict. > > The static keyword has a completely different meaning when used inside > a function: > > int f() > { > static int call_counter = 0; > // lifetime: global (until the app exits) > // visibility: local (for this function only) > ++call_counter; > } > > This means the variable keeps its value in between function calls, but > it still remains private to the f() function. You can't possibly access > call_counter outside of f(), but due to its static nature, the variable > stays alive, even when the function returns. > > Functions can also be declared static. Functions have visibility only, > no lifetime, so static only controls whether a function is hidden inside > a .c file, or visible across the entire project. > > In C++ the static keyword has a whole bunch of other meanings. > A static member means it belongs to the class, not to the instance. > As you see, the static keyword is always interpreted > differently, depending on the context in which it appears. > This is valuable information for me since this wasn't exactly the case in the old non C compliant compiler. Thanks Tom! Robert
From: Robby on 8 Feb 2010 18:11 "Tim Roberts" wrote: > Robby <Robby(a)discussions.microsoft.com> wrote: > > > >But just to reminisse here, so then lets do everything with functions. Why > >bother with macros at all. Why even keep macros in the C language standard. > > Yes, indeed. MANY people have asked that exact question, including Bjarne > Stroustroup, the guy who created C++. He wanted to get rid of the > preprocessor altogether. Interesting! > Templates and "const" variables are both attempts to eliminate misused >preprocessor features. and inline functions too, right? > I don't see the difference between "going through a bunch of functions to > find the one" and "going through a bunch of macros to find the one". The > search process is the same, and the function choice is safer. I never said a *bunch* of macros, since I too tend to limit their use... especially with all the macro bashing that's been going on :-). A few, (15-20) macros that carry out simple tasks of a very general settings or calcs involving the *system's* setups for peripherals or so, I wouldn't mind ! So this way I would know, if its something to do with a simple system's setup, I would look into the macros. But then again, as Igor says, why wouldn't I use functions. Thanks for your post Tim. Regards Rob
First
|
Prev
|
Pages: 1 2 3 4 Prev: Using pointer-to-member of incomplete type in constructor call in VS2008 Next: filename string manipulation |