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 ... .

Parents Reply Children
  • But if your #pragma only handles the next variable declaration, you could need a huge number of #pragma - and each one can be wrong.

    Think about C++ which can use { and } to group information into a namespace or class. Or have private:, public:, protected: to have lots of things into a specific access category.

    If you think dynamic allocation of objects is a requirement to get the full power of C++, then you may have used C++, but you have not considered the full richness of the language compared to C.

    Think about a circular buffer with enquque(), dequeue(), is_empty(), is_full(), have_data(). Duplicated multiple times for multiple serial ports. And having the constructor receive a pointer to a char buffer and a buffer size. So you can instantiate - statically - multiple ring buffers of different size, for UART0 receive, UART0 transmit, UART1 receive, UART1 transmit, ..., SPI0 receive, SPI0 transmit, ... Zero dynamic memory involved. Unless you decide that you have one big block of buffer memory, and a configuration table to decide how many bytes of the pool for UART0 receive, UART0 transmit, ...

    Or let's say that you have a class with a state machine supporting different sets of blinks - slow, fast, dual-blink, ... And a virtual method for driving an output. So you have one instance driving LED-1, one for LED-2, one for REL-1, one for REL-2, ... And the code runs through all these objects (accessed as an array of pointers) and call an update() method - automagically getting all your blinks etc. No dynamic memory involved. With C, you would have to send a pointer to a struct containing configuration and current tick counters, and a function pointer for what output to drive.

    Dynamic memory is for solving problems where you don't know how many of something you need. But with limited RAM, you can't just allocate more objects. So there must be limits. And limits means that the problem could have been solved statically. Or that you know that you either need x objects of type 1, or y objects of type 2 - but not at the same time. C or C++ doesn't matter. You still have to write the software based on the existing hw - and select the used hw based on needs. But operator new is not what "makes" C++. The main purpose of it is to allow many programs to share a big processor and have each individual program claim no more than what is needed. With smaller microcontrollers, there are only one program running. So no memory to save for another program. So rules for how to share the available RAM can be decided at design time - without hurting the capabilities of the programming language.

  • ok ok you are starting to convince me ...

    but do you perhaps also have an answer on my new question? (possibility to define the exact time point, when the constructor of a global class is called? - if I cannot influence this, I would prefer to somehow not allow constructors for global classes ... (in Windows programming this is one of the rules: never use a global class, otherwise you might run in strange problems ... if here I HAVE TO use global classes (at least for the classes with much data, because I do not want dynamic memory allocation), then I would at least like to have this "global class constructor problem" under tight control))).

  • But you have realized that the ARM processor don't care much about global memory or stack memory - so you can create objects on the stack?

    You can even do something like:

    void main(void) {
        if (configured_as_x) {
            CFunctionality1 app;
            app.Run();
        } else {
            CFunctionality2 app;
            app.Run();
        }
    }