This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Restrict Global Variables to specific modules

Here comes a very special wish - please do not call me crazy:

Working with many C modules, I prefer to define protected/"dangerous" variables static to one module. Quite often I would like to have a variable global only in a restricted number of modules - typically only in one module and then further the the communication modules (e. g. "comm1.c", "comm2.c").

To achieve this, it would be terrifically nice, if I could create the following code with a special pragma modifier (I made the examples with two modules "comm1.c" and "comm2.c", to keep the examples more general - but in fact I would be perfectly happy, if this would work with restriction to one further module "comm.c"):

#pragma RestrictedGlobal "comm1.c"
#pragma RestrictedGlobal "comm2.c"
int iRestricted;

(alternatively also like this (but then it would not work in other C compilers):

#pragma allowAccess "comm1.c"
#pragma allowAccess "comm2.c"
static int iRestricted;


)

In "comm1.c", "comm2.c" then the extern command

extern int iRestricted;


should be allowed - but it any other module it should fail (best would be, if the command itself would be allowed, but if the access to iRestricted would fail in any other module - then it is possible to include the extern declaration in some header file without the danger of failure)

I know that this is not possible in any other C compiler, but I think pragma is not standardized, so C compiler producers can add their own - just I think better use the first of the above alternatives, as then the code will also work in other C compilers not recognizing this pragma.

... I know that this might be quite an effort, as it concerns the very interior of the compiling/linking tables ... so just a polite question ... .

  • please do not call me crazy:

    All right --- I'll just call you misguided then.

    I know that this is not possible in any other C compiler,

    Forget about C compilers --- this would be in clear and brutal violation of the principles of C itself. So if you really think you need stuff like that, maybe you should reconsider your choice of programming language. Using C++, with namespaces and friend declarations, what you want just might be possible.

    But at the heart of it, this wish is misguided. If you don't have the discipline to keep yourself and co-workers from violating rules such as "you do not #include other module's private headers" or "never use 'extern' in *.c files", then you'll have much bigger problems than this one anyway ... so even if some compiler had this feature, that wouldn't solve the actual structural problem with the way your team develops code.

  • Hi,
    I know, that this wish is really very far going.

    But your answer is also a bit too much. It really would be very nice. I use C++ in Win programming extensively. But switching to C++ only to get private or protected variable access seems to be a bit overdrawn from my point of view.

    It is not, that I could not install such rules - currently I do this (it is not so much for other colleagues, but also for me, if I come back to coding in a project some years later).

    But of course I just prefer very much, if such rules are somehow fixed "in an intrinsic way" in the program source code - especially in case of controller development which should be as fail-prove as possible.

  • "But switching to C++ only to get private or protected variable access seems to be a bit overdrawn from my point of view."

    So totally wrong view.

    C++ is designed for encapsulation. So it is a natural choice to go for C++ if encapsulation is a need. Not try to turn C inside out and make it into something it is never intended to be.

    C++ is not less efficient as programming language for an ARM chip. And it is designed to allow mixing of C and C++ code, to allow gradual migration.

    You currently are using nails but dislike how you have to hammer them down. You want the nail with a twist feature because you feel that would work better in your work process. But you refuse to switch to screws just because you prefer the hammer instead of having to use a screw driver. So you then ask for someone to modify the hammer to somehow twist down your strangely modified nails. Sorry, but that is a very twisted view.

    Use a normal hammer with normal nails.

    Use a normal screwdriver with normal screws.

  • So do you use C++ for ARM programming? (with Keil compiler?)

  • That depends on project and needs.

  • I'm not sure something like an MPU suits your needs...?

  • I just want to use "normal" (=state of the art) Microcontroller. And I need a very good compiler like Keil.

    Is it possible to use C++ with Keil - would you recommend it?

  • Just look at the description on the Product page...

  • "Normal" and "state-of-the-art" need not be the same thing.

    Most people do _not_ want to use a "state-of-the-art" chip - that would often be a too expensive solution. Why use a $10,000,000 car to drive to/from work, just because it is state-of-the-art?

    Haven't you already been recommended to use C++, in case you need encapsulation? Why not evaluate it yourself instead of trying to ask other what they - based on their completely different experience and needs - think about using it?

  • I just looked through the product page, and to my surprise it really seems to support C++ completely. ... .

    Just e. g. for my demo board MCBSTM32F400 I did not find any C++ code - also I never saw any uController project in C++ up to now - this is completely new to me. Therefore I really would be interested if somebody has used C++ already for uController development, and what the experiences are (I would like to start "from the scratch" - too much respect concerning the pitfalls of uController programming).

  • ... sorry ... last message should say "I would NOT like to start from the scratch ...".

  • STL with lots of dynamic memory etc may not be a good choice.

    And it may be a good idea to stay away from streams etc also, to keep down on size of linked libraries.

    But C++ instead of C really doesn't matter much to a microcontroller as general as the ARM. You can write your programs as C programs with objects. Or C programs with namespaces. Or C programs with references. Or C programs with exceptions.

    Good or bad experiences aren't really caused by the language, but how the user decides to use the language.

    The main requirement for the processor is that it should have a full set of instructions for work through pointers, allowing good mapping of the "this" pointer that is a very important part of object-oriented C++ code.

  • So you would recommend to keep the assemply and C startup code from the basic example, and then start with the classes only in the main function and above?

    I agree with you, that I anyway would not dare to include too much dynamic memory handling into the controller software.

    So then I would mainly use the C++ classes in a sort of quasi-static approach (possibly even with most member functions static ...).

    Just for now I think this is a bit too much for me, I have to think about it some time.

    Anyway it would be nice, if Keil could present some simple C++ demo, maybe based on Blinky, to show the advantages and possible pitfalls of C++ concerning controller programming.

  • Classes are for C++ - not assembler.

    So you will obviously not play around with any classes in the startup file.

    You don't need static member methods. But you should think twice about how you allocate your objects. If they are global objects. Or created on the stack. Or from a special memory pool. Or from standard heap. Or if you are going to use something else.

    What you have to realize is that the operator "new" isn't the only way to get object variables in C++.

  • What you have to realize is that the operator "new" isn't the only way to get object variables in C++.

    That's what I meant with my statement "I would presumably use C++ in a sort of quasi-static approach".

    If I skip new, then usually I would define all my classes global. And in this case of course it will not make much sense to use constructor / destructor functions. So the main advantage of using C++ then will restrict to the variable protection scheme of C++, which of course is very advanced.

    ... perhaps in some future I will try some small steps with some simple global C++ classes without constructor / destructor ...