Alternative to passing context as a parameter

This is a discussion on Alternative to passing context as a parameter within the C Programming forums, part of the General Programming Boards category; Hi, I am working on a C application. The application can have multiple instances. In each instance of the application, ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    Lightbulb Alternative to passing context as a parameter

    Hi,

    I am working on a C application. The application can have multiple instances. In each instance of the application, there are 4 components. One of the four components will have a structure that needs to be made available to the other 3 components.

    Multiple instances of the application (due to reasons beyond my control) will use SHARED memory for globals. So, if a global variable was to be used, the multiple instances of the application (with 4 components each) would interfere with each other via the global variable - as far as I can tell. The other way I can think of is to pass the structure to every function call. So the structure can propagate deep into the call stack.

    So,
    1) Global variable - potential interference from multiple instances of application
    2) Passing the struct as a parameter in EACH and EVERY function call in each component - tedious and ugly

    What do you guys think? Any suggestions?. Thank you!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Passing context as a parameter is definitely a good proposition - yes, you will be passing it around a lot, but as long as it's a pointer to the context, it should be fine doing that - not much overhead at all.

    Another possibility is to have a "handle", such as an integer index into the relevant context - again, you will probably be passing this variable around a lot too.

    Which is better will depend on your design, really.

    Passing context as a parameter (pointer to struct, generally) is a very common solution, and if you ever work on for example device drivers in Windows, nearly every single function takes a "PDEV" parameter, which is a pointer to a DEV structure (device dependent data - MS accepts one when the driver is initialized, and then passes it along whenever the driver is called - and you usually need it everywhere in the driver except for the odd rare utility function [e.g. some function that calculates some common calculation] ). Linux device drivers have a very similar design - the name is different, but the concept is certainly the same.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    Hi Mats,

    Thank you for your reply. So, based on your response, passing the context seems to be a common way of doing it. I guess there is no way of avoiding it then. I guess it is very similar to how C++ passes the "this" pointer implicitly.

    Now, is the shared memory for global variables (wrt multiple instances of the application) common? How are the local variables guaranteed to be safe? - i.e. how are the stack pointers used by different instances of the application guaranteed to be different, when they clearly interfere in the global space? Finally, does this apply to "static local variables" in functions as well?

    Thank you!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I expect that this is some sort of embedded system.

    No, it is not common to share global data. Local static normally end up in the same chunk of memory as regulars - the only difference is that the name isn't available to other functions.

    Stacks, to work correctly, must be unshared.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    Hi Mats,

    It's actually a Windows system. And isn't local static variable persistent and retain its value? i.e. it has local scope but has the lifetime of the program.

    I "presumed" it would be treated as a global (life-time wise), but the compiler enforces local scope rules...

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  5. typecasting data for parameter passing
    By daluu in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 01:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21